当前位置:网站首页>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后台推送消息
边栏推荐
- 8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
- Discourse Custom Header Links
- StringJoiner详解
- 7、私信列表
- Basic learning about Redis related content
- JetPack组件Databinding
- 4. Sensitive word filtering (prefix tree)
- 8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
- 刚出道“一战成名”,安全、舒适一个不落
- 12 Disk related commands
猜你喜欢
![[Android] Room - Alternative to SQLite](/img/52/0bc1c0a3173da6d39224ad8440a462.png)
[Android] Room - Alternative to SQLite

12 Disk related commands

Mysql 45讲学习笔记(二十四)MYSQL主从一致

What is distributed and clustered?What is the difference?

品牌广告投放平台的中台化应用与实践

局域网电脑硬件信息收集工具

字体压缩神器font-spider的使用

【编译原理】词法分析程序设计原理与实现

Linux下redis7的安装,启动与停止

Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
随机推荐
6、显示评论和回复
Graphical lower_bound & upper_bound
STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode
Chapter 9 SVM实践
学习DAVID数据库(1)
QML的使用
[C language foundation] Solve C language error: expected ';', ',' or ')' before '&' token
【HCIP】ISIS
11、Redis实现关注、取消关注以及关注和粉丝列表
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
Installation of mysql5.7.37 under CentOS7 [perfect solution]
VS QT——ui不显示新添加成员(控件)||代码无提示
知识蒸馏7:知识蒸馏代码详解
共模电感的仿真应用来了,满满的干货送给大家
Face detection based on opencv
How to build a private yum source
Basic learning about Redis related content
Uninstallation of mysql5.7.37 under CentOS7 [perfect solution]
SQL注入 Less54(限制次数的SQL注入+union注入)
加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还