1 创建项目,添加依赖库,修改pom.xml
org.springframework.boot
spring-boot-starter-web
2.4.2
org.projectlombok
lombok
1.18.20
provided
org.springframework.boot
spring-boot-starter-websocket
2.3.5.RELEASE
2 创建WebSocket配置类
package com.demo.cfg;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import javax.annotation.Resource;
/**
* 代码描述: websocket配置类
*
* @author wujianping
* @since 2022/5/16 14:25
*/
@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
@Resource
WebSocketHandler defaultHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(defaultHandler, "/ws").addInterceptors().setAllowedOrigins("*");
}
}
3 创建websocket操作句柄
package com.demo.handler;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
/**
* 代码描述: 自定义处理器需要实现 WebSocketHandler 接口
*
* @author wujianping
* @since 2022/5/16 14:28
*/
@Component
public class DefaultHandler implements WebSocketHandler {
/**
* 建立连接
* @param session
* @throws Exception
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// 缓存用户信息: userInfo
}
/**
* 接收消息
* @param session
* @param message
* @throws Exception
*/
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage> message) throws Exception {
}
/**
* 发生错误
* @param session
* @param exception
* @throws Exception
*/
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
// 清除用户缓存信息
}
/**
* 关闭连接
* @param session
* @param closeStatus
* @throws Exception
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
// 清除用户缓存信息
}
@Override
public boolean supportsPartialMessages() {
return false;
}
}
4 创建拦截器
package com.demo.handler;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import java.util.Map;
/**
* 代码描述: 拦截器可以在 websocket 连接握手阶段做一些校验,还可以存储用户的连接信息,返回 false 拒绝连接,true 则通过
*
* @author wujianping
* @since 2022/5/16 14:29
*/
@Component
public class DefaultInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception {
// TODO
return false; // [1]
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
}
}
5 创建测试页面
菜鸟教程(runoob.com)