当前位置:网站首页>WebSocket Session is null
WebSocket Session is null
2022-07-31 03:02:00 【Gerard pique, thanks】
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");
}
}
}
WebSocketused in service class
在@ServerEndpoint(value = “/websocket/{name}”)中加入configurator = HttpSessionWSHelper.class
@Slf4j
@Component
@ServerEndpoint(value = "/websocket/{name}",configurator = HttpSessionWSHelper.class)
public class WebSocket {
}
SpringBoot_WebSocket_JavaPush messages in the background
边栏推荐
- 4、敏感词过滤(前缀树)
- TCP详解(二)
- 公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
- 图解lower_bound&upper_bound
- TCP/IP四层模型
- The whole process scheduling, MySQL and Sqoop
- Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
- What is a distributed lock?Three ways of implementing distributed lock
- 什么是系统?
- 华为分布式存储FusionStorage知识点总结【面试篇】
猜你喜欢
YOLOV5学习笔记(三)——网络模块详解
The principle of complete replication of virtual machines (cloud computing)
What is distributed and clustered?What is the difference?
接口测试关键技术
Huawei od dice js
Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
Discourse Custom Header Links
加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还
随机推荐
SQL注入 Less47(报错注入) 和Less49(时间盲注)
Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest
字体压缩神器font-spider的使用
多线程下类对象的服务承诺探讨
如何搭建私有yum源
Installation of mysql5.7.37 under CentOS7 [perfect solution]
YOLOV5学习笔记(三)——网络模块详解
2022牛客多校联赛第四场 题解
19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
12 磁盘相关命令
[Godot][GDScript] 二维洞穴地图随机生成
选好冒烟测试用例,为进入QA的制品包把好第一道关
STM32问题合集
Local area network computer hardware information collection tool
TCP详解(三)
Difference between CMOS and TTL?
经典链表OJ强训题——快慢双指针高效解法
【C语言】表达式求值的一般方法
The principle of complete replication of virtual machines (cloud computing)
IIR滤波器和FIR滤波器