首页>代码>spring mvc基于webSocket实现用户聊天通信的一个小demo>/websocket/src/main/java/org/xdemo/example/websocket/websocket/HandShake.java
package org.xdemo.example.websocket.websocket;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
/**
* Socket建立连接(握手)和断开
*
* @author Goofy
* @Date 2015年6月11日 下午2:23:09
*/
public class HandShake implements HandshakeInterceptor {
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
System.out.println("Websocket:用户[ID:" + ((ServletServerHttpRequest) request).getServletRequest().getSession(false).getAttribute("uid") + "]已经建立连接");
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession(false);
// 标记用户
Long uid = (Long) session.getAttribute("uid");
if(uid!=null){
attributes.put("uid", uid);
}else{
return false;
}
}
return true;
}
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
}
}
最近下载更多
yuanshun LV7
2023年6月2日
人工智能4708 LV11
2023年5月8日
ewan007 LV30
2023年4月21日
lironggang LV38
2023年2月16日
136542892@qq.com LV23
2022年6月20日
Wave666 LV6
2022年3月21日
15380502522 LV2
2021年6月30日
lris_luanling LV11
2021年4月21日
a1677596408 LV23
2021年4月15日
宋国斌 LV4
2021年3月17日
最近浏览更多
3334004690 LV11
2024年5月28日
Luck_ZDM LV12
2024年5月22日
Xiaobaiya11 LV2
2024年3月4日
yhwyhw1 LV2
2023年12月21日
小新Coding LV9
2023年9月7日
微信网友_6627705560322048 LV1
2023年8月30日
wanfeng_233 LV4
2023年8月29日
你好啊呐 LV19
2023年8月23日
yuanshun LV7
2023年6月2日
tianli3000 LV8
2023年5月15日

