位置:
ruoyi-common
模块的pom.xml
文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
如果是新建模块,则需要在
ruoyi-admin
的pom.xml
中导入新建模块,否则注解不生效
<dependency>
<groupId>com.ruoyi</groupId>
<!-- 新module的名称 -->
<artifactId>ruoyi-websocket</artifactId>
</dependency>
位置:ruoyi-framework/src/main/java/com/ruoyi/framework/config/MyWebSocketConfig.java
package com.ruoyi.framework.config;
import com.ruoyi.framework.interceptor.MyWebSocketInterceptor;
import com.ruoyi.framework.web.exception.MyWebSocketHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class MyWebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
System.out.println("--- success register websocket --- ");
//固定连接路径则使用/ws,如/ws后还有参数,则追加/*通配符
registry.addHandler(new MyWebSocketHandler(), "/ws")//设置连接路径和处理器
.setAllowedOrigins("*")//允许跨域访问
.addInterceptors(new MyWebSocketInterceptor());//设置拦截器
}
}
位置:ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/MyWebSocketHandler.java
package com.ruoyi.framework.web.exception;
import org.springframework.web.socket.*;
public class MyWebSocketHandler implements WebSocketHandler {
//建立新的 socket 连接后回调的方法
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
System.out.println("this is a new socket: 建立新连接");
//从session中获取存放的参数
session.getAttributes().get("id");
}
// 接收客户端发送的 Socket
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
// 连接发送的消息
String msg = message.getPayload().toString();
// 通过session向连接发送消息
session.sendMessage(new TextMessage("开发喵:send message"));
}
//连接出错时,回调的方法
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
System.out.println("开发喵收到的错误信息:" + exception);
}
//连接关闭时,回调的方法
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
System.out.println("断开连接");
}
// WebSocketHandler 是否处理部分消息 默认返回false即可
@Override
public boolean supportsPartialMessages() {
return false;
}
}
位置:ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/MyWebSocketInterceptor.java
package com.ruoyi.framework.interceptor;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import java.util.Map;
public class MyWebSocketInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest servletRequest,
ServerHttpResponse servletResponse,
WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {
System.out.println("websocket前置拦截");
//如果用到Sec-WebSocket-Protocol,可以采用getHeaders().get(key)的方法获取
if (servletRequest.getHeaders().get("Sec-WebSocket-Protocol") == null) {
System.out.println("无Sec-WebSocket-Protocol,进行拦截!");
return false;
}
String protocol = servletRequest.getHeaders().get("Sec-WebSocket-Protocol").get(0);
//如果uri的路径中带有参数,可获取到uri字符串,采用截取的方式处理,最后存入attributes
String path = servletRequest.getURI().getPath();
String id = path.substring(path.lastIndexOf('/') + 1);
attributes.put("id", id);
System.out.println("设备id:" + id);
return true;
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
System.out.println("websocket后置拦截");
}
}
出现--- success register websocket ---
说明websocket
已经集成。
powered by kaifamiao