当前位置:网站首页>WebSocket Session为null
WebSocket Session为null
2022-07-31 03:00:00 【杰拉德·皮克·谢】
WebSocket Session为null
引入HttpSessionWSHelper类
/** * @program: * @description: 获取session * @author: Xzj * @create: 2021-01-20 15:05 **/
package com.pm.common.socket;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
public class HttpSessionWSHelper extends ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
System.out.println("调用modifyHandshake方法...");
HttpSession session = (HttpSession) request.getHttpSession();//session有可能为空
if (session!=null){
System.out.println("获取到session id:"+session.getId());
sec.getUserProperties().put(HttpSession.class.getName(),session);
}else{
System.out.println("modifyHandshake 获取到null session");
}
}
}
WebSocket服务类中使用
在@ServerEndpoint(value = “/websocket/{name}”)中加入configurator = HttpSessionWSHelper.class
@Slf4j
@Component
@ServerEndpoint(value = "/websocket/{name}",configurator = HttpSessionWSHelper.class)
public class WebSocket {
}
SpringBoot_WebSocket_Java后台推送消息
边栏推荐
- Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
- StringJoiner详解
- YOLOV5 study notes (3) - detailed explanation of network module
- Android's webview cache related knowledge collection
- Compile Hudi
- return in try-catch
- CMOS和TTL的区别?
- 15、网站统计数据
- Modbus on AT32 MCU
- execsnoop tool
猜你喜欢

Software accumulation -- Screenshot software ScreenToGif

CefSharp入门-winform

【C语言】进制转换一般方法

Moxa NPort device flaw could expose critical infrastructure to devastating attack

6、显示评论和回复

YOLOV5 study notes (2) - environment installation + operation + training

The simulation application of common mode inductance is here, full of dry goods for everyone

10 Permission introduction
![[Android] Room - Alternative to SQLite](/img/52/0bc1c0a3173da6d39224ad8440a462.png)
[Android] Room - Alternative to SQLite

Linux下redis7的安装,启动与停止
随机推荐
What is distributed and clustered?What is the difference?
QML的使用
Discussion on Service Commitment of Class Objects under Multithreading
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
PMP微信群日常习题
Mysql 45讲学习笔记(二十四)MYSQL主从一致
SQL注入 Less54(限制次数的SQL注入+union注入)
[C language foundation] Solve C language error: expected ';', ',' or ')' before '&' token
数学解决——环形链表问题
YOLOV5 study notes (2) - environment installation + operation + training
多线程下类对象的服务承诺探讨
11. Redis implements follow, unfollow, and follow and follower lists
STM32问题合集
自动化办公案例:如何自动生成期数据?
共模电感的仿真应用来了,满满的干货送给大家
Local area network computer hardware information collection tool
CorelDRAW2022 streamlined Asia Pacific new features in detail
Draw Your Cards
C#远程调试
测试中的误报和漏报同样的值得反复修正