WebSocket一种网络通信协议,主要用于服务端和客户端双向通信,一般多于多人聊天、实时监控、消息推送等方面,客户端使用前端实现,本文不做描述,主要写一下webSocket服务端是怎么实现的。
1、引入依赖
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2、往spring容器中注入 ServerEndpointExporter
webSocketConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
/*
往spring容器中注入 ServerEndpointExporter
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3、Endpoint具体实现

本文详细介绍了WebSocket协议在服务端的实现过程,包括引入依赖、配置Spring容器、Endpoint的具体实现、服务启动以及如何使用在线工具进行测试。
最低0.47元/天 解锁文章
6628

被折叠的 条评论
为什么被折叠?



