5 changed files with 119 additions and 1 deletions
@ -0,0 +1,48 @@ |
|||||
|
package yq.framework.web.webSocket; |
||||
|
|
||||
|
import org.springframework.web.socket.*; |
||||
|
|
||||
|
/** |
||||
|
* @auther yq |
||||
|
* @data 2025/1/13 |
||||
|
*/ |
||||
|
public class MyWebSocketHandler implements WebSocketHandler { |
||||
|
|
||||
|
//建立新的 socket 连接后回调的方法
|
||||
|
@Override |
||||
|
public void afterConnectionEstablished(WebSocketSession session) throws Exception { |
||||
|
System.out.println("新连接"); |
||||
|
//从session中获取存放的参数
|
||||
|
session.getAttributes().get("id"); |
||||
|
} |
||||
|
|
||||
|
// 接收客户端发送的 Socket
|
||||
|
@Override |
||||
|
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { |
||||
|
// 连接发送的消息
|
||||
|
String msg = message.getPayload().toString(); |
||||
|
System.out.println("收到的消息:" + msg+"来自客户端:"+session.getId()); |
||||
|
// 通过session向连接发送消息
|
||||
|
session.sendMessage(new TextMessage("我收到了你的消息,感谢你的来信")); |
||||
|
} |
||||
|
|
||||
|
//连接出错时,回调的方法
|
||||
|
@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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,39 @@ |
|||||
|
package yq.framework.web.webSocket; |
||||
|
|
||||
|
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; |
||||
|
|
||||
|
/** |
||||
|
* @auther yq |
||||
|
* @data 2025/1/13 |
||||
|
*/ |
||||
|
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后置拦截"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
package yq.framework.web.webSocket; |
||||
|
|
||||
|
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; |
||||
|
|
||||
|
/** |
||||
|
* @auther yq |
||||
|
* @data 2025/1/13 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@EnableWebSocket |
||||
|
public class SpringWebSocketConfig implements WebSocketConfigurer { |
||||
|
|
||||
|
@Override |
||||
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
||||
|
System.out.println("注册websocket"); |
||||
|
//固定连接路径则使用/ws,如/ws后还有参数,则追加/*通配符
|
||||
|
registry.addHandler(new MyWebSocketHandler(), "/ws/*")//设置连接路径和处理器
|
||||
|
.setAllowedOrigins("*") //允许跨域访问
|
||||
|
.addInterceptors(new MyWebSocketInterceptor());//设置拦截器
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue