zoukankan      html  css  js  c++  java
  • WebSocket之获取HttpSession

    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);
        }
  • 相关阅读:
    Charles:rewrite重写功能
    Vue中provide和inject 用法
    vue中install方法
    vue自定义组件(通过Vue.use()来使用)即install的使用
    Eelectron 中的remote模块
    理解Vue中的Render渲染函数
    Vue.js中this.$nextTick()的使用
    postman请求本地接口Error: connect ECONNREFUSED 127.0.0.1:8083
    1016 Phone Bills (25 分)
    CF842E Nikita and game
  • 原文地址:https://www.cnblogs.com/zhaoww/p/5119706.html
Copyright © 2011-2022 走看看