Spring Boot 整合單機websocket(附github原始碼)

websocket 概念

websocket 是一個通訊協議,透過單個 TCP 連線提供全雙工通訊。websocket 連線成功後,服務端和客戶可以進行雙向通訊。不同於 http 通訊協議需要每次由客戶端發起,服務響應到客戶端。

websocket 相對輪詢也能節約頻寬,並且能實時的進行通訊。

整合步驟

1。 新增 maven 依賴

org。springframework。boot spring-boot-starter-web org。springframework。boot spring-boot-starter-websocket 2。1。3。RELEASE org。springframework。boot spring-boot-starter-freemarker 2。1。3。RELEASE

新增web、websocket和freemarker依賴。

2。 使用 ServerEndpointExporter 建立 bean

這個 bean 會自動註冊宣告 @ServerEndpoint 註解宣告的 websocket endpoint,使用springboot自帶tomcat啟動需要該配置,使用獨立 tomcat 則不需要該配置。

@Configurationpublic class WebSocketConfig { //tomcat啟動無需該配置 @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }}

3。 建立服務端端點 (ServerEndpoint)

@Component@ServerEndpoint(value = “/message”)@Slf4jpublic class WebSocket { private static Map webSocketSet = new ConcurrentHashMap<>(); private Session session; @OnOpen public void onOpen(Session session) throws SocketException { this。session = session; webSocketSet。put(this。session。getId(),this); log。info(“【websocket】有新的連線,總數:{}”,webSocketSet。size()); } @OnClose public void onClose(){ String id = this。session。getId(); if (id != null){ webSocketSet。remove(id); log。info(“【websocket】連線斷開:總數:{}”,webSocketSet。size()); } } @OnMessage public void onMessage(String message){ if (!message。equals(“ping”)){ log。info(“【wesocket】收到客戶端傳送的訊息,message={}”,message); sendMessage(message); } } /** * 傳送訊息 * @param message * @return 全部都發送一遍 */ public void sendMessage(String message){ for (WebSocket webSocket : webSocketSet。values()) { webSocket。session。getAsyncRemote()。sendText(message); } log。info(“【wesocket】廣播訊息,message={}”, message); }}

4。 新增 controller 和 客戶端

新增 controller

@GetMapping({“”,“index。html”})public ModelAndView index() { ModelAndView view = new ModelAndView(“index”); return view;}

新增ftl頁面

<!DOCTYPE html> websocket

原文連結:https://www。cnblogs。com/jeremylai7/p/15378748。html