当前位置:网站首页>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后台推送消息
边栏推荐
- Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph
- TCP/IP four-layer model
- Mathematical Ideas in AI
- try-catch中含return
- Chapter 9 SVM实践
- YOLOV5学习笔记(三)——网络模块详解
- 公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
- mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
- execsnoop 工具
- 共模电感的仿真应用来了,满满的干货送给大家
猜你喜欢
Basic learning about Redis related content
8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
4. Sensitive word filtering (prefix tree)
Linux下redis7的安装,启动与停止
The Sad History of Image Processing Technology
10 权限介绍
【C语言】进制转换一般方法
12 Disk related commands
7. List of private messages
图解lower_bound&upper_bound
随机推荐
JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
【shell基础】判断目录是否为空
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
YOLOV5学习笔记(三)——网络模块详解
Number 16, top posts
SQL注入 Less54(限制次数的SQL注入+union注入)
C primer plus学习笔记 —— 8、结构体
SQL注入 Less47(报错注入) 和Less49(时间盲注)
Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢
Go 项目实战-获取多级分类下的全部商品
YOLOV5 study notes (2) - environment installation + operation + training
Android's webview cache related knowledge collection
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
f.grid_sample
golang GUI for nuxui — HelloWorld
12 Disk related commands
10 权限介绍
Mysql 45讲学习笔记(二十四)MYSQL主从一致
Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest