日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot使用Websocket

發(fā)布時間:2025/3/12 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot使用Websocket 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

webSocket是HTML5的一種新協(xié)議,它實現(xiàn)了服務(wù)端與客戶端的全雙工通信,建立在傳輸層,tcp協(xié)議之上,即瀏覽器與服務(wù)端需要先建立tcp協(xié)議,再發(fā)送webSocket連接建立請求。webSocket的連接:客戶端發(fā)送請求信息,服務(wù)端接受到請求并返回相應(yīng)的信息。連接建立。客戶端發(fā)送http請求時,通過 Upgrade:webSocket Connection:Upgrade 告知服務(wù)器需要建立的是webSocket連接,并且還會傳遞webSocket版本號,協(xié)議的字版本號,原始地址,主機地址, webSocket相互通信的Header很小,大概只有2Bytes

SpringBoot結(jié)合WebSocket實現(xiàn)群發(fā)消息

一. 消息群發(fā)

1. 導(dǎo)入依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId> </dependency>

2. 新建WebSocket配置類

@Configuration public class WebsocketConfiguration {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();} }

3. 新建WebSocket服務(wù)端

@ServerEndpoint("/test") @Component @Slf4j public class MyWebsocketServer {/*** 存放所有在線的客戶端*/private static Map<String, Session> clients = new ConcurrentHashMap<>();@OnOpenpublic void onOpen(Session session) {log.info("有新的客戶端連接了: {}", session.getId());//將新用戶存入在線的組clients.put(session.getId(), session);}/*** 客戶端關(guān)閉* @param session session*/@OnClosepublic void onClose(Session session) {log.info("有用戶斷開了, id為:{}", session.getId());//將掉線的用戶移除在線的組里clients.remove(session.getId());}/*** 發(fā)生錯誤* @param throwable e*/@OnErrorpublic void onError(Throwable throwable) {throwable.printStackTrace();}/*** 收到客戶端發(fā)來消息* @param message 消息對象*/@OnMessagepublic void onMessage(String message) {log.info("服務(wù)端收到客戶端發(fā)來的消息: {}", message);this.sendAll(message);}/*** 群發(fā)消息* @param message 消息內(nèi)容*/private void sendAll(String message) {for (Map.Entry<String, Session> sessionEntry : clients.entrySet()) {sessionEntry.getValue().getAsyncRemote().sendText(message);}} }

4. 啟動后,客戶端連接websocket

5. 客戶端發(fā)送消息得到的響應(yīng)和服務(wù)端的響應(yīng)

  • 客戶端
  • 服務(wù)端

SpringBoot結(jié)合WebSocket實現(xiàn)一對一發(fā)送消息

導(dǎo)入依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId> </dependency> <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId> </dependency>

新建Websocket配置類

@Configuration public class WebsocketConfiguration {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();} }

新建消息對象

@Data @Builder @NoArgsConstructor @AllArgsConstructor public class Message {private String userId;private String message; }

新建websocket服務(wù)

@ServerEndpoint("/test-one") @Component @Slf4j public class MyOneToOneServer {/*** 用于存放所有在線客戶端*/private static Map<String, Session> clients = new ConcurrentHashMap<>();private Gson gson = new Gson();@OnOpenpublic void onOpen(Session session) {log.info("有新的客戶端上線: {}", session.getId());clients.put(session.getId(), session);}@OnClosepublic void onClose(Session session) {String sessionId = session.getId();log.info("有客戶端離線: {}", sessionId);clients.remove(sessionId);}@OnErrorpublic void onError(Session session, Throwable throwable) {throwable.printStackTrace();if (clients.get(session.getId()) != null) {clients.remove(session.getId());}}@OnMessagepublic void onMessage(String message) {log.info("收到客戶端發(fā)來的消息: {}", message);this.sendTo(gson.fromJson(message, Message.class));}/*** 發(fā)送消息** @param message 消息對象*/private void sendTo(Message message) {Session s = clients.get(message.getUserId());if (s != null) {try {s.getBasicRemote().sendText(message.getMessage());} catch (IOException e) {e.printStackTrace();}}} }

測試

  • 上線兩個客戶端
  • 使用其中一個客戶端發(fā)送消息給另外一個,只需要帶上另外一個客戶端的id
  • 接收方客戶端

總結(jié)

以上是生活随笔為你收集整理的SpringBoot使用Websocket的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。