WebSocket协议,客户端和服务端都可以主动的推送消息,可以是文本也可以是二进制数据。而且没有同源策略的限制,不存在跨域问题。协议的标识符就是ws。链接
WebSocket介绍
WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议,能更好的节省服务器资源和带宽,并且能够更实时地进行通讯。
SpringBoot集成WebSocket
第一步:新建一个spring boot项目july-websocket,引入websocket依赖包,代码如下:
org.springframework.bootgroupId> spring-boot-starter-websocketartifactId> dependency>
第二步:创建sebsocket配置类,主要用于检测注解@ServerEndpoint的bean并注册它们,代码如下:
package com.july.test.config;
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 ServerEndpointExpo