WebSocket之获取HttpSession

由于WebSocket与Http协议的不同,故在使用常用的HttpSession方面就存在了一些问题。通过google翻阅到了在onOpen方法下使用HttpSession的方法。

新建一个GetHttpSessionConfigurator类并继承Configurator类

package per.zww.web;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;
/*
* 获取HttpSession
*
*/ public class GetHttpSessionConfigurator extends Configurator { @Override
public void modifyHandshake(ServerEndpointConfig sec,
HandshakeRequest request, HandshakeResponse response) {
// TODO Auto-generated method stub
HttpSession httpSession=(HttpSession) request.getHttpSession();
sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
} }

然后在@ServerEndpoint注解里面添加configurator属性

@ServerEndpoint(value="/socketTest",configurator=GetHttpSessionConfigurator.class)

在onOpen方法里加入参数EndpointConfig config即可获取HttpSession

  @OnOpen
public void onOpen(Session session,EndpointConfig config) {
HttpSession httpSession= (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
System.out.println( httpSession.getAttribute("name"));
sessionMap.put(session.getId(), session);
}

WebSocket之获取HttpSession的更多相关文章

  1. java使用websocket,并且获取HttpSession,源码分析

    转载请在页首注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/6238826.html 一:本文使用范围 此文不仅仅局限于spring boot,普通的sprin ...

  2. WebSocket获取httpSession空指针异常的解决办法

    小坑:使用requestListner解决不了这个问题! 如何获取HttpSession 在使用webSocket实现p2p或者一对多聊天功能的时候我们经常会有这样的需求:webSocket服务端需要 ...

  3. Springboot-WebSocket获取HttpSession问题

    换了新工作,第一个任务就是和这个有关,以前没接触过,没办法,各种度娘.谷哥,大部分都是只言片语,要么就是特定的配置环境还不贴配置--,踩坑无数, 遂整理成笔记 WebSocket协议 WebSocke ...

  4. java使用Websocket获取HttpSession出现的问题与解决

    websocket的写法就不多说了,主要记一记其中出现的问题 1.获取不到httpSession 解决办法:先重写握手方法,将httpsession放入ServerEndpointConfig.get ...

  5. WebSocket学习记录

    参考资料: Java后端WebSocket的Tomcat实现 基于Java的WebSocket推送 java WebSocket的实现以及Spring WebSocket 利用spring-webso ...

  6. springboot--websocket简单demo(一):session chat

    最近跟着大佬 https://tycoding.cn/2019/06/16/project/boot-chat/ 敲了2个关于websocket的demo,总结一下 从将会话信息保存在session中 ...

  7. webSocket 使用 HttpSession 的数据配置与写法

    1.前言 webSoket 无法获取 HttpSession  ,使用就更谈不上了 !!! 2解决过程 使用   configurator  注入即可 (1) 配置一个类 1 package cn.c ...

  8. websocket总结

    一.WebSocket简介 WebSocket  protocol是HTML5一种新的协议,WebSocket 是目前唯一真正实现全双工通信的服务器向客户端推送的互联网技术.WebSocket的出现使 ...

  9. websocket(二)--简单实现网页版群聊

    websocket可以实现服务端的消息推送,而不必在客户端轮询,大大的节省的资源,对于实时通讯来说简直是个大喜讯. 在上一篇文章中介绍了协议握手,这篇文章将通过实现简单的群聊来帮助进一步了解webso ...

随机推荐

  1. Atitit.获取验证码图片通过web

    Atitit.获取验证码图片通过web 1. WebRequest进行较为底层的访问(不推荐) 1 2. WebBrowser截图 1 3. 剪贴板复制法Clipboard(推荐) 1 4. C# 取 ...

  2. android自定义控件(1)-自定义控件属性

    那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...

  3. CControlLayer

    #ifndef __CONTROLLAYER_H__ #define __CONTROLLAYER_H__ #include "XWidget.h" class CMapDialo ...

  4. OGNL表达式的基本语法和用法

    首先我们一起来看一下OGNL中的#.%和$符号. 关于OGNL各种用法总结参看:http://blog.163.com/seara520@126/blog/static/720693042010320 ...

  5. 1、改变 vs编辑器的主题

    打开 visual studio, 在菜单栏选择  工具 -> 扩展和更新 -> 输入 “color theme” 安装完成后,选择样式. 选择好样式后, vs 立即改变主题.再次打开样式 ...

  6. python学习笔记(7)--爬虫隐藏代理

    说明: 1. 好像是这个网站的代理http://www.xicidaili.com/ 2. 第2,3行的模块不用导入,之前的忘删了.. 3. http://www.whatismyip.com.tw/ ...

  7. mvc 返回list数据 页面 mode

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<实体命名空间& ...

  8. Spring.Net框架一:Spring.Net简介

    一.Spring.Net简介 Spring.NET为建立企业级应用提供了一套轻量级的解决方案.通过Spring.NET,我们可以用统一且透明的方式来配置应用程序.Spring.NET的重点是为中间层提 ...

  9. 后缀数组LCP + 二分 - UVa 11107 Life Forms

    Life Forms Problem's Link Mean: 给你n个串,让你找出出现次数大于n/2的最长公共子串.如果有多个,按字典序排列输出. analyse: 经典题. 直接二分判断答案. 判 ...

  10. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...