SpringBoot集成WebSocket的兩種方式(JDK內(nèi)置版和Spring封裝版)
本站在2014年4月時(shí)曾全面的學(xué)習(xí)HTML5的技術(shù),特寫過HTML5的WebSocket示例,當(dāng)時(shí)使用的Servlet3.0規(guī)范中的API,需要Tomcat7的支持(貌似在Tomcat6的后期維護(hù)版本也增加了WebSocket的支持),早在當(dāng)初該示例還是本站的一個(gè)特色功能,好多來找我要源碼的呢。時(shí)隔多年再來使用SpringBoot架構(gòu)來體驗(yàn)一下集成WebSocket的實(shí)現(xiàn),經(jīng)過一番資料的百科大概有找到使用兩種方式的實(shí)現(xiàn),我分別對(duì)它們進(jìn)行了實(shí)踐,所以我稱這兩種方式為JDK內(nèi)置版和Spring封裝版。
1.JDK內(nèi)置版
主要是使用javax.websocket包下的注解進(jìn)行集成,主要有:ServerEndpoint、OnOpen、OnMessage、OnClose、OnError相關(guān)的類和注解來集成,整體上比較簡(jiǎn)單,都是基于注解定義的方法來聲明的,參考如下代碼所示:
JdkWebSocket
package cn.chendd.websocket.jdk; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; /** * websocket實(shí)現(xiàn) * * @date 2023/6/2 21:02 */ @Component @ServerEndpoint(value = "/websocket/jdk") public class JdkWebSocket { @OnOpen public void onOpen(Session session) { System.out.println("WebSocketConfig.onOpen"); } @OnMessage public void onMessage(Session session , String message) { System.out.println("WebSocketConfig.onMessage-->" + session + "--->" + message); } @OnClose public void onClose() { System.out.println("WebSocketConfig.onClose"); } @OnError public void onError(Session sesison , Throwable throwable) { System.out.println("WebSocketConfig.onError-->" + sesison + "--->" + throwable); } }
JdkWebSocketConfig
package cn.chendd.websocket.jdk; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * @author chendd * @date 2023/6/2 21:15 */ @Configuration public class JdkWebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter(){ return new ServerEndpointExporter(); } }
運(yùn)行結(jié)果
2.Spring封裝版
Spring封裝版本有對(duì)于消息類型進(jìn)行封裝,提供了更加全面的WebSocket方面的API,值得深入分析。
SpringWebSocketHandler
package cn.chendd.websocket.spring; import org.springframework.stereotype.Component; import org.springframework.web.socket.*; import org.springframework.web.socket.handler.TextWebSocketHandler; /** * SpringWebSocketHandler * * @author chendd * @date 2023/6/2 22:08 */ @Component public class SpringWebSocketHandler extends TextWebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { super.afterConnectionEstablished(session); System.out.println("SpringWebSocketHandler.afterConnectionEstablished"); } @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { super.handleMessage(session, message); System.out.println("SpringWebSocketHandler.handleMessage"); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { super.handleTextMessage(session, message); System.out.println("SpringWebSocketHandler.handleTextMessage"); } @Override protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception { super.handlePongMessage(session, message); System.out.println("SpringWebSocketHandler.handlePongMessage"); } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { super.handleTransportError(session, exception); System.out.println("SpringWebSocketHandler.handleTransportError"); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { super.afterConnectionClosed(session, status); System.out.println("SpringWebSocketHandler.afterConnectionClosed"); } @Override public boolean supportsPartialMessages() { System.out.println("SpringWebSocketHandler.supportsPartialMessages"); return super.supportsPartialMessages(); } }
SpringWebSocketConfig
package cn.chendd.websocket.spring; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import javax.annotation.Resource; /** * SpringWebSocketConfig * * @author chendd * @date 2023/6/2 22:11 */ @Configuration @EnableWebSocket public class SpringWebSocketConfig implements WebSocketConfigurer { @Resource private SpringWebSocketHandler springWebSocketHandler; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(springWebSocketHandler , "/websocket/spring").setAllowedOrigins("*"); } }
運(yùn)行結(jié)果
3.其它說明
(1)本次示例重點(diǎn)在于服務(wù)端Java代碼的示例,未編寫前端示例,可在下方的項(xiàng)目源碼中參見前端的HTML集成,或者是使用本例中使用的在線WebSocket測(cè)試的頁面地址進(jìn)行在線驗(yàn)證,右鍵查看其源代碼也是原生的HTML5規(guī)范的相關(guān)代碼;
(2)提供了JDK的內(nèi)置版本和Spring的封裝版本,個(gè)人推薦使用Spring的封裝版,畢竟JDK的注解不夠清晰的描述出被注解的方法的方法具體參數(shù),而Spring封裝版本有對(duì)于消息類型進(jìn)行封裝;
(3)代碼比較簡(jiǎn)單,結(jié)合前文的參考鏈接中的全量代碼也包含在內(nèi),代碼包結(jié)構(gòu)如下:
參考文章鏈接:一個(gè)猜你所選的小程序隨寫
到此這篇關(guān)于SpringBoot集成WebSocket的兩種方式的文章就介紹到這了,更多相關(guān)SpringBoot集成WebSocket內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Jenkins+Maven+Gitea+Nexus搭建CICD環(huán)境的方式
這篇文章主要介紹了基于Jenkins+Maven+Gitea+Nexus從0到1搭建CICD環(huán)境,大家都知道Nexus是一套“開箱即用”的系統(tǒng)不需要數(shù)據(jù)庫,它使用文件系統(tǒng)加Lucene來組織數(shù)據(jù),需要的朋友可以參考下2022-01-01深入理解Java設(shè)計(jì)模式之職責(zé)鏈模式
這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解2021-11-11Spring配置多個(gè)數(shù)據(jù)源并實(shí)現(xiàn)數(shù)據(jù)源的動(dòng)態(tài)切換功能
這篇文章主要介紹了Spring配置多個(gè)數(shù)據(jù)源并實(shí)現(xiàn)數(shù)據(jù)源的動(dòng)態(tài)切換功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01Spring項(xiàng)目運(yùn)行依賴spring-contex解析
這篇文章主要介紹了Spring項(xiàng)目運(yùn)行依賴spring-contex解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05spring+springmvc+mybatis 開發(fā)JAVA單體應(yīng)用
這篇文章主要介紹了spring+springmvc+mybatis 開發(fā)JAVA單體應(yīng)用的相關(guān)知識(shí),本文通過圖文實(shí)例代碼的形式給大家介紹的非常詳細(xì) ,需要的朋友可以參考下2018-11-11Java 生產(chǎn)者/消費(fèi)者問題實(shí)例詳解
這篇文章主要實(shí)例分析了java中生產(chǎn)者消費(fèi)者問題的方法,需要的朋友可以可以參考下2017-04-04使用Java橋接模式打破繼承束縛優(yōu)雅實(shí)現(xiàn)多維度變化
這篇文章主要為大家介紹了使用Java橋接模式打破繼承束縛,優(yōu)雅實(shí)現(xiàn)多維度變化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05