SpringBoot3集成WebSocket的全過程
一、簡介
WebSocket通過一個TCP連接在客戶端和服務器之間建立一個全雙工、雙向的通信通道,使得客戶端和服務器之間的數(shù)據(jù)交換變得更加簡單,允許服務端主動向客戶端推送數(shù)據(jù),在WebSocket的API中,瀏覽器和服務器只需要完成一次握手,兩者之間就直接可以創(chuàng)建持久性的連接,并進行雙向數(shù)據(jù)傳輸。
【基于Postman工具的WebSocket交互】
Connected to ws://127.0.0.1:8088/web/socket/msg Handshake Details Request URL: http://127.0.0.1:8088/web/socket/msg Request Method: GET Status Code: 101 Request Headers Sec-WebSocket-Version: 13 Sec-WebSocket-Key: 5Qrs8JeRLsiY9G/PRJUocQ== Connection: Upgrade Upgrade: websocket Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits Host: 127.0.0.1:8088 Response Headers Upgrade: websocket Connection: upgrade Sec-WebSocket-Accept: E3aFw2bBzxByPCynmQ7lZ+7BgsU= Sec-WebSocket-Extensions: permessage-deflate;client_max_window_bits=15
二、工程搭建
1、工程結構
2、依賴管理
在starter-websocket
的依賴中,涉及到spring
框架中兩個關系較為密切的組件,分別是websocket
和messaging
組件。
<!--WebSocket--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>${spring-boot.version}</version> </dependency>
三、WebSocket用法
1、示意圖
在下面的案例中,大致模擬這樣一個流程,三個客戶端分別和服務端建立連接,然后進行客戶端之間的會話通信。
2、API參考
這里通過4個核心的方法注解,分別處理會話的不同動作,比如連接的建立和關閉,通信交互和錯誤處理;在實際的應用場景中,需要在通信方法中設計更多的指令來應對不同的業(yè)務場景。
@ServerEndpoint("/web/socket/msg") public class MsgWebSocket { private static final ConcurrentHashMap<String,Session> sessions = new ConcurrentHashMap<>(); private static final AtomicInteger onlineCount = new AtomicInteger(0); /** * 建立連接調(diào)用的方法 */ @OnOpen public void onOpen(Session session) { String userId = session.getRequestParameterMap().get("userId").get(0); // 加入Set中 sessions.put(userId,session); // 在線數(shù)增加 onlineCount.getAndIncrement(); log.info("session-{},online-count-{}",session.getId(),onlineCount.get()); } /** * 客戶端消息處理的方法 */ @OnMessage public void sendMsg(Session sender,String message) throws Exception { MsgDTO dto = JSONUtil.toBean(message, MsgDTO.class); Session receiver = sessions.get(dto.getUserId()); if (receiver != null) { receiver.getBasicRemote().sendText(dto.getMsg()); } } /** * 關閉連接調(diào)用的方法 */ @OnClose public void onClose(Session session) { String userId = session.getRequestParameterMap().get("userId").get(0); // 從Set中刪除 sessions.remove(userId); // 在線數(shù)減少 onlineCount.getAndDecrement(); log.info("session-{},down-line-count-{}",session.getId(),onlineCount.get()); } /** * 發(fā)生錯誤調(diào)用的方法 */ @OnError public void onError(Session session, Throwable throwable) throws Exception { log.error("Web Stock Error", throwable); session.getBasicRemote().sendText(throwable.getMessage()); } }
測試效果圖:注意這里使用的是postman最新版本。
四、總結
到此這篇關于SpringBoot3集成WebSocket的全過程的文章就介紹到這了,更多相關SpringBoot3集成WebSocket內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot整合Netty+Websocket實現(xiàn)消息推送的示例代碼
- springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available)
- SpringBoot 整合WebSocket 前端 uniapp 訪問的詳細方法
- Springboot整合WebSocket實戰(zhàn)教程
- SpringBoot整合WebSocket的客戶端和服務端的實現(xiàn)代碼
- SpringBoot整合Netty實現(xiàn)WebSocket的示例代碼
- springboot整合websocket最基礎入門使用教程詳解
- 通過實例講解springboot整合WebSocket
- SpringBoot3整合WebSocket詳細指南
相關文章
使用Java代碼將IP地址轉(zhuǎn)換為int類型的方法
這篇文章主要介紹了使用Java代碼將IP地址轉(zhuǎn)換為int類型的方法,這也是各大計算機考試和ACM以及面試的常見基礎問題,需要的朋友可以參考下2015-08-08SpringBoot項目集成Swagger和swagger-bootstrap-ui及常用注解解讀
這篇文章主要介紹了SpringBoot項目集成Swagger和swagger-bootstrap-ui及常用注解解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java實現(xiàn)獲取前、后N天日期的函數(shù)分享
本文給大家分享的是使用java實現(xiàn)的獲取當前日期前后N天的函數(shù),非常的簡單實用,有需要的小伙伴可以參考下。2015-03-03SQL Server 2000 Driver for JDBC Service Pack 3 安裝測試方法
這篇文章主要介紹了數(shù)據(jù)庫連接測試程序(SQL Server 2000 Driver for JDBC Service Pack 3 安裝測試),需要的朋友可以參考下2014-10-10EasyExcel工具讀取Excel空數(shù)據(jù)行問題的解決辦法
EasyExcel是阿里巴巴開源的一個excel處理框架,以使用簡單,節(jié)省內(nèi)存著稱,下面這篇文章主要給大家介紹了關于EasyExcel工具讀取Excel空數(shù)據(jù)行問題的解決辦法,需要的朋友可以參考下2022-08-08zuulGateway 通過filter統(tǒng)一修改返回值的操作
這篇文章主要介紹了zuulGateway 通過filter統(tǒng)一修改返回值的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10