上海網(wǎng)站建設(shè)哪個(gè)平臺(tái)好百度推廣怎么優(yōu)化
概述
基于spingboot的websocket多人聊天系統(tǒng)。包括訂閱,廣播、點(diǎn)對(duì)點(diǎn)單人聊天,多人聊天室功能。
詳細(xì)
一、運(yùn)行效果
簡單示例
廣播
單人聊天
多人聊天室
二、相關(guān)代碼
websocket配置
package com.iamgpj.demowebsocket.config;import com.iamgpj.demowebsocket.v4.SocketChannelInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;import javax.websocket.server.ServerEndpointConfig;/*** @author Ives* @date 2020/4/20 13:51*/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends ServerEndpointConfig.Configurator implements WebSocketMessageBrokerConfigurer {/*** 注冊端點(diǎn),發(fā)布或者訂閱消息的時(shí)候需要連接此端點(diǎn)* addEndpoint websocket的端點(diǎn),客戶端需要注冊這個(gè)端點(diǎn)進(jìn)行鏈接* setAllowedOrigins 非必須,*表示允許其他域進(jìn)行連接,跨域* withSockJS 允許客戶端利用sockjs進(jìn)行瀏覽器兼容性處理* @param registry*/@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint("/ws/endpoints-websocket")//.setAllowedOrigins("*").withSockJS();}/*** 配置消息代理* @param registry*/@Overridepublic void configureMessageBroker(MessageBrokerRegistry registry) {// 設(shè)置服務(wù)器廣播消息的基礎(chǔ)路徑registry.enableSimpleBroker("/topic", "/user");// 設(shè)置客戶端訂閱消息的基礎(chǔ)路徑registry.setApplicationDestinationPrefixes("/app");}/** 配置頻道攔截器,用于聊天室 */@Overridepublic void configureClientInboundChannel(ChannelRegistration registration) {registration.interceptors(new SocketChannelInterceptor());}
}
聊天室程序
package com.iamgpj.demowebsocket.v4;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;/*** @author Ives* @date 2020/4/21 11:13*/
@Service
@Slf4j
public class ChatRoomService {@Autowiredprivate SimpMessagingTemplate template;/*** 模擬數(shù)據(jù)庫中存儲(chǔ)的用戶*/public static Map<String, String> userMap = new HashMap<>();/*** 保存當(dāng)前在線用戶* 實(shí)際應(yīng)該存儲(chǔ)于redis等可持久化緩存服務(wù)器中,做到數(shù)據(jù)共享*/public static Map<String, String> onlineUserMap = new HashMap<>();/** 初始化數(shù)據(jù) */static {userMap.put("張三", "111111");userMap.put("李四", "111111");userMap.put("王五", "111111");userMap.put("趙六", "111111");userMap.put("陳七", "111111");}/*** 每兩秒給客戶端推送當(dāng)前在線用戶*/@Scheduled(fixedRate = 2000)public void pushOnlineUser() {Collection<String> users = onlineUserMap.values();log.info("【聊天室在線用戶】={}", users);template.convertAndSend("/topic/onlineUser", users);}/*** 推送聊天消息* @param inMessage 內(nèi)容*/public void pushChatRoom(InMessage inMessage) {OutMessage outMessage = new OutMessage();BeanUtils.copyProperties(inMessage, outMessage);outMessage.setPushTime(LocalDateTime.now());template.convertAndSend("/topic/chatRoom", outMessage);}
}
三、代碼結(jié)構(gòu)
四、相關(guān)說明
1、項(xiàng)目前段使用bootstrap框架,使用maven引入webjars作為樣式文件。
2、訪問路徑分別為
????http://localhost:8080/v1/index.html
????http://localhost:8080/v2/index.html
????http://localhost:8080/v3/tom.html
????http://localhost:8080/v3/jerry.html
????http://localhost:8080/v4/index.html
3、聊天室請分別使用不同瀏覽器登錄測試。登錄名可以為,張三、李四、王五、趙六....