org.springframework.boot
spring-boot-starter-websocket
加入配置类
@Configuration
public class WebSocketConfig {
/**
-
注入ServerEndpointExporter,
-
这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
加入连接发送消息方法
@Component
@ServerEndpoint(“/websocket/{userName}”)
// 此注解相当于设置访问URL
public class WebSocket {
private Session session;
private static CopyOnWriteArraySet webSockets = new CopyOnWriteArraySet<>();
private static Map<String, Session> sessionPool = new HashMap<String, Session>();
private final static Logger logger = LoggerFactory.getLogger(LoginIntercept.class);