java后端+前端使用WebSocket實現(xiàn)消息推送的詳細流程
前言
在項目的開發(fā)時,遇到實現(xiàn)服務器主動發(fā)送數(shù)據(jù)到前端頁面的功能的需求。實現(xiàn)該功能不外乎使用輪詢和websocket技術,但在考慮到實時性和資源損耗后,最后決定使用websocket?,F(xiàn)在就記錄一下用Java實現(xiàn)Websocket技術吧~
Java實現(xiàn)Websocket通常有兩種方式:1、創(chuàng)建WebSocketServer類,里面包含open、close、message、error等方法;2、利用Springboot提供的webSocketHandler類,創(chuàng)建其子類并重寫方法。我們項目雖然使用Springboot框架,不過仍采用了第一種方法實現(xiàn)。
創(chuàng)建WebSocket的簡單實例操作流程
1.引入Websocket依賴
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>2.7.0</version> </dependency>
2.創(chuàng)建配置類WebSocketConfig
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * 開啟WebSocket支持 */ @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
3.創(chuàng)建WebSocketServer
在websocket協(xié)議下,后端服務器相當于ws里面的客戶端,需要用@ServerEndpoint指定訪問路徑,并使用@Component注入容器
@ServerEndpoint:當ServerEndpointExporter類通過Spring配置進行聲明并被使用,它將會去掃描帶有@ServerEndpoint注解的類。被注解的類將被注冊成為一個WebSocket端點。所有的配置項都在這個注解的屬性中
( 如:@ServerEndpoint(“/ws”) )
下面的栗子中@ServerEndpoint指定訪問路徑中包含sid,這個是用于區(qū)分每個頁面
import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.net.Socket; import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** * @ServerEndpoint 注解是一個類層次的注解,它的功能主要是將目前的類定義成一個websocket服務器端, * 注解的值將被用于監(jiān)聽用戶連接的終端訪問URL地址,客戶端可以通過這個URL來連接到WebSocket服務器端 */ @ServerEndpoint("/notice/{userId}") @Component @Slf4j public class NoticeWebsocket { //記錄連接的客戶端 public static Map<String, Session> clients = new ConcurrentHashMap<>(); /** * userId關聯(lián)sid(解決同一用戶id,在多個web端連接的問題) */ public static Map<String, Set<String>> conns = new ConcurrentHashMap<>(); private String sid = null; private String userId; /** * 連接成功后調(diào)用的方法 * @param session * @param userId */ @OnOpen public void onOpen(Session session, @PathParam("userId") String userId) { this.sid = UUID.randomUUID().toString(); this.userId = userId; clients.put(this.sid, session); Set<String> clientSet = conns.get(userId); if (clientSet==null){ clientSet = new HashSet<>(); conns.put(userId,clientSet); } clientSet.add(this.sid); log.info(this.sid + "連接開啟!"); } /** * 連接關閉調(diào)用的方法 */ @OnClose public void onClose() { log.info(this.sid + "連接斷開!"); clients.remove(this.sid); } /** * 判斷是否連接的方法 * @return */ public static boolean isServerClose() { if (NoticeWebsocket.clients.values().size() == 0) { log.info("已斷開"); return true; }else { log.info("已連接"); return false; } } /** * 發(fā)送給所有用戶 * @param noticeType */ public static void sendMessage(String noticeType){ NoticeWebsocketResp noticeWebsocketResp = new NoticeWebsocketResp(); noticeWebsocketResp.setNoticeType(noticeType); sendMessage(noticeWebsocketResp); } /** * 發(fā)送給所有用戶 * @param noticeWebsocketResp */ public static void sendMessage(NoticeWebsocketResp noticeWebsocketResp){ String message = JSONObject.toJSONString(noticeWebsocketResp); for (Session session1 : NoticeWebsocket.clients.values()) { try { session1.getBasicRemote().sendText(message); } catch (IOException e) { e.printStackTrace(); } } } /** * 根據(jù)用戶id發(fā)送給某一個用戶 * **/ public static void sendMessageByUserId(String userId, NoticeWebsocketResp noticeWebsocketResp) { if (!StringUtils.isEmpty(userId)) { String message = JSONObject.toJSONString(noticeWebsocketResp); Set<String> clientSet = conns.get(userId); if (clientSet != null) { Iterator<String> iterator = clientSet.iterator(); while (iterator.hasNext()) { String sid = iterator.next(); Session session = clients.get(sid); if (session != null) { try { session.getBasicRemote().sendText(message); } catch (IOException e) { e.printStackTrace(); } } } } } } /** * 收到客戶端消息后調(diào)用的方法 * @param message * @param session */ @OnMessage public void onMessage(String message, Session session) { log.info("收到來自窗口"+this.userId+"的信息:"+message); } /** * 發(fā)生錯誤時的回調(diào)函數(shù) * @param error */ @OnError public void onError(Throwable error) { log.info("錯誤"); error.printStackTrace(); } }
封裝了一個發(fā)送消息的對象可以直接使用
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data @ApiModel("ws通知返回對象") public class NoticeWebsocketResp<T> { @ApiModelProperty(value = "通知類型") private String noticeType; @ApiModelProperty(value = "通知內(nèi)容") private T noticeInfo; }
4.websocket調(diào)用
一個用戶調(diào)用接口,主動將信息發(fā)給后端,后端接收后再主動推送給指定/全部用戶
@RestController @RequestMapping("/order") public class OrderController { @GetMapping("/test") public R test() { NoticeWebsocket.sendMessage("你好,WebSocket"); return R.ok(); } }
前端WebSocket連接
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>SseEmitter</title> </head> <body> <div id="message"></div> </body> <script> var limitConnect = 0; init(); function init() { var ws = new WebSocket('ws://192.168.2.88:9060/notice/1'); // 獲取連接狀態(tài) console.log('ws連接狀態(tài):' + ws.readyState); //監(jiān)聽是否連接成功 ws.onopen = function () { console.log('ws連接狀態(tài):' + ws.readyState); limitConnect = 0; //連接成功則發(fā)送一個數(shù)據(jù) ws.send('我們建立連接啦'); } // 接聽服務器發(fā)回的信息并處理展示 ws.onmessage = function (data) { console.log('接收到來自服務器的消息:'); console.log(data); //完成通信后關閉WebSocket連接 // ws.close(); } // 監(jiān)聽連接關閉事件 ws.onclose = function () { // 監(jiān)聽整個過程中websocket的狀態(tài) console.log('ws連接狀態(tài):' + ws.readyState); reconnect(); } // 監(jiān)聽并處理error事件 ws.onerror = function (error) { console.log(error); } } function reconnect() { limitConnect ++; console.log("重連第" + limitConnect + "次"); setTimeout(function(){ init(); },2000); } </script> </html>
項目啟動,打開頁面后控制臺打印連接信息
調(diào)用order/test方法后前端打印推送消息內(nèi)容
這樣,就可以接口或者ws調(diào)用網(wǎng)址的方式進行websocket的通信啦~
如果沒有前端頁面也可以使用在線WebSocket測試
總結
到此這篇關于java后端+前端使用WebSocket實現(xiàn)消息推送的文章就介紹到這了,更多相關javaWebSocket實現(xiàn)消息推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Java中使用WebSocket的幾種方式
- Java實現(xiàn)使用Websocket發(fā)送消息詳細代碼舉例
- 如何在Java中使用WebSocket協(xié)議
- springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available)
- Java WebSocket客戶端接收大量數(shù)據(jù)的三種方案
- 使用Java WebSocket獲取客戶端IP地址的示例代碼
- Java中實現(xiàn)WebSocket方法詳解
- 教你如何使用Java實現(xiàn)WebSocket
- 一步步教你如何使用Java實現(xiàn)WebSocket
- JAVA 日常開發(fā)中Websocket示例詳解
相關文章
Spring Data JPA進行數(shù)據(jù)分頁與排序的方法
這篇文章主要介紹了Spring Data JPA進行數(shù)據(jù)分頁與排序的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11