当前位置:网站首页>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
边栏推荐
猜你喜欢

CentOS7下mysql5.7.37的安装【完美方案】

华为分布式存储FusionStorage知识点总结【面试篇】

关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法

12 磁盘相关命令

编译Hudi

4、敏感词过滤(前缀树)

12 Disk related commands

知识蒸馏7:知识蒸馏代码详解

Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)

What is a distributed lock?Three ways of implementing distributed lock
随机推荐
SQL注入 Less54(限制次数的SQL注入+union注入)
SQL injection Less54 (limited number of SQL injection + union injection)
golang GUI for nuxui — HelloWorld
数学解决——环形链表问题
CentOS7下mysql5.7.37的安装【完美方案】
Android's webview cache related knowledge collection
经典链表OJ强训题——快慢双指针高效解法
YOLOV5 study notes (2) - environment installation + operation + training
SQL注入 Less47(报错注入) 和Less49(时间盲注)
编译Hudi
C#远程调试
【CocosCreator 3.5】CocosCreator 获取网络状态
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
CentOS7下mysql5.7.37的卸载【完美方案】
The simulation application of common mode inductance is here, full of dry goods for everyone
LeetCode简单题之找到和最大的长度为 K 的子序列
Multilingual settings of php website (IP address distinguishes domestic and foreign)
Compile Hudi
CMOS和TTL的区别?
Modbus on AT32 MCU