当前位置:网站首页>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后台推送消息
边栏推荐
- JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
- Number 16, top posts
- VS QT——ui不显示新添加成员(控件)||代码无提示
- 接口测试关键技术
- 7、私信列表
- PMP微信群日常习题
- Why is String immutable?
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- 经典链表OJ强训题——快慢双指针高效解法
- 【Bank Series Phase 1】People's Bank of China
猜你喜欢
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
11、Redis实现关注、取消关注以及关注和粉丝列表
工程(五)——小目标检测tph-yolov5
Moxa NPort device flaw could expose critical infrastructure to devastating attack
Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest
STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode
Mathematics to solve the problem - circular linked list
The application of AI in the whole process of medical imaging equipment
局域网电脑硬件信息收集工具
随机推荐
CorelDRAW2022精简亚太新增功能详细介绍
接口测试关键技术
【C语言】表达式求值的一般方法
原子操作 CAS
execsnoop 工具
【Bank Series Phase 1】People's Bank of China
10、Redis实现点赞(Set)和获取总点赞数
【Android】Room —— SQLite的替代品
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
10 Permission introduction
C#远程调试
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
多线程下类对象的服务承诺探讨
Map.Entry理解和应用
JetPack组件Databinding
The modification is not properly placed in the sandbox, causing Apple compatibility issues
选好冒烟测试用例,为进入QA的制品包把好第一道关
Draw Your Cards
全流程调度——MySQL与Sqoop