SpringBoot整合Websocket
整合步骤很简单,请看下文分解。👇
-
引入Maven依赖,如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>2.3.3.RELEASE</version> </dependency>
-
新建一个类:WebSocketTextMsgConfig,实现WebSocketConfigurer接口,如下:
package com.example.textMessageWebsocket; import org.springframework.beans.factory.annotation.Autowired; 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; @Configuration @EnableWebSocket public class WebSocketTextMsgConfig implements WebSocketConfigurer { @Autowired WebSocketTextmsgHandler webSocketTextmsgHandler; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry