当前位置:网站首页>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后台推送消息
边栏推荐
- Multilingual settings of php website (IP address distinguishes domestic and foreign)
- Basic learning about Redis related content
- VS QT——ui不显示新添加成员(控件)||代码无提示
- Maximum area of solar panel od js
- Installation, start and stop of redis7 under Linux
- mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
- 品牌广告投放平台的中台化应用与实践
- Discourse 自定义头部链接(Custom Header Links)
- 15、网站统计数据
- SQL注入 Less47(报错注入) 和Less49(时间盲注)
猜你喜欢

Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest

Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢

Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!

Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击

19.支持向量机-优化目标和大间距直观理解

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

Discourse Custom Header Links

【编译原理】递归下降语法分析设计原理与实现

7、私信列表

10. Redis implements likes (Set) and obtains the total number of likes
随机推荐
TCP/IP four-layer model
Installation, start and stop of redis7 under Linux
Clustering index, and what is the difference between a clustering index
12 磁盘相关命令
编译Hudi
11、Redis实现关注、取消关注以及关注和粉丝列表
Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest
Modbus on AT32 MCU
Graphical lower_bound & upper_bound
【CV项目调试】CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT问题
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
The modification is not properly placed in the sandbox, causing Apple compatibility issues
golang GUI for nuxui — HelloWorld
Number 16, top posts
Compile Hudi
Thesis framework of the opening report
The use of font compression artifact font-spider
16、热帖排行
Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢