SpringBoot整合WebSocket實現(xiàn)后端向前端主動推送消息方式
一、引入websocket依賴
<dependency> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
二、WebSocket配置
@Configuration public class WebSocketConfig { ? ? @Bean ? ? public ServerEndpointExporter serverEndpointExporter() { ? ? ? ? return new ServerEndpointExporter(); ? ? } }
三、WebSocket服務(wù)
(前端連接地址ws://ip:端口/websocket,請自行替換ip、端口和接口名稱)
@ServerEndpoint(value = "/websocket") @Component public class WebSocketServer { ? ? private final static Logger log = LoggerFactory.getLogger(WebSocketServer.class); ? ? ? //靜態(tài)變量,用來記錄當前在線連接數(shù)。應該把它設(shè)計成線程安全的。 ? ? private static int onlineCount = 0; ? ? //concurrent包的線程安全Set,用來存放每個客戶端對應的MyWebSocket對象。 ? ? private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>(); ? ? ? //與某個客戶端的連接會話,需要通過它來給客戶端發(fā)送數(shù)據(jù) ? ? private Session session; ? ? ? /** ? ? ?* 連接建立成功調(diào)用的方法 ? ? ?*/ ? ? @OnOpen ? ? public void onOpen(Session session) { ? ? ? ? this.session = session; ? ? ? ? //加入set中 ? ? ? ? webSocketSet.add(this); ? ? ? ? //在線數(shù)加1 ? ? ? ? addOnlineCount(); ? ? ? ? log.info("有新連接加入!當前在線人數(shù)為" + getOnlineCount()); ? ? ? ? try { ? ? ? ? ? ? sendMessage("連接成功"); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? log.error("websocket IO異常"); ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 連接關(guān)閉調(diào)用的方法 ? ? ?*/ ? ? @OnClose ? ? public void onClose() { ? ? ? ? //從set中刪除 ? ? ? ? webSocketSet.remove(this); ? ? ? ? //在線數(shù)減1 ? ? ? ? subOnlineCount(); ? ? ? ? log.info("有一連接關(guān)閉!當前在線人數(shù)為" + getOnlineCount()); ? ? } ? ? ? /** ? ? ?* 收到客戶端消息后調(diào)用的方法 ? ? ?* ? ? ?* @param message 客戶端發(fā)送過來的消息 ? ? ?*/ ? ? @OnMessage ? ? public void onMessage(String message, Session session) { ? ? ? ? log.info("來自客戶端的消息:" + message); ? ? ? ? ? //群發(fā)消息 ? ? ? ? for (WebSocketServer item : webSocketSet) { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? item.sendMessage(message); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? /** ? ? ?* @param session ? ? ?* @param error ? ? ?*/ ? ? @OnError ? ? public void onError(Session session, Throwable error) { ? ? ? ? log.error("發(fā)生錯誤"); ? ? ? ? error.printStackTrace(); ? ? } ? ? ? public void sendMessage(String message) throws IOException { ? ? ? ? this.session.getBasicRemote().sendText(message); ? ? } ? ? ? /** ? ? ?* 群發(fā)自定義消息 ? ? ?*/ ? ? public static void sendInfo(String message) throws IOException { ? ? ? ? log.info(message); ? ? ? ? for (WebSocketServer item : webSocketSet) { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? item.sendMessage(message); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? public static synchronized int getOnlineCount() { ? ? ? ? return onlineCount; ? ? } ? ? ? public static synchronized void addOnlineCount() { ? ? ? ? WebSocketServer.onlineCount++; ? ? } ? ? ? public static synchronized void subOnlineCount() { ? ? ? ? WebSocketServer.onlineCount--; ? ? } }
四、消息推送
后端調(diào)用WebServer的sendInfo接口(例如:WebSocketServer.sendInfo("Hello World");)實現(xiàn)主動向前端推送消息
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
這篇文章主要介紹了SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Eclipse+Java+Swing實現(xiàn)學生成績管理系統(tǒng)的實例代碼
這篇文章主要介紹了Eclipse+Java+Swing實現(xiàn)學生成績管理系統(tǒng),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01基于Java語言MD5加密Base64轉(zhuǎn)換方法
這篇文章主要為大家詳細介紹了基于Java語言的MD5加密Base64轉(zhuǎn)換方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09java數(shù)據(jù)結(jié)構(gòu)之二分查找法 binarySearch的實例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)之二分查找法 binarySearch的實例的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10Java8 stream 中利用 groupingBy 進行多字段分組求和案例
這篇文章主要介紹了Java8 stream 中利用 groupingBy 進行多字段分組求和案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08