Spring boot 集成websocket的实现方式
WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。
maven依赖
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.4.1</version>
</dependency>
配置文件config
import org.java_websocket.client.WebSocketClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
//客户端
@Bean
public WebSocketClient webSocketClient() {
try {
MyWebSocketClient webSocketClient = MyWebSocketClient.getInstance();
webSocketClient.connect();
return webSocketClient;
} catch (Exception e) {
e.printStackTrace();
}