Websocket - httpSession 返回 null [英] Websocket - httpSession returns null

查看:36
本文介绍了Websocket - httpSession 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 websocket 握手\会话和 HttpSession 对象之间建立连接.

I would like to make the connection between a websocket handshake \ session to a HttpSession object.

我使用了以下握手修改:

I've used the following handshake modification:

public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator
{
@Override
public void modifyHandshake(ServerEndpointConfig config, 
                            HandshakeRequest request, 
                            HandshakeResponse response)
{
    HttpSession httpSession = (HttpSession)request.getHttpSession();
    config.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}

正如这篇文章中提到的:从 Web Socket @ServerEndpoint 中的 HttpServletRequest 访问 HttpSession

As mentioned in this post: Accessing HttpSession from HttpServletRequest in a Web Socket @ServerEndpoint

现在,握手时由于某种原因,(HttpSession)request.getHttpSession() 一直返回null.

Now, For some reason on the hand shake, the (HttpSession)request.getHttpSession() returns null all the time.

这是我的客户端代码:

<!DOCTYPE html>
<html>
<head>
<title>Testing websockets</title>
</head>
<body>
<div>
    <input type="submit" value="Start" onclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
    var webSocket = 
        new WebSocket('ws://localhost:8080/com-byteslounge-websockets/websocket');

    webSocket.onerror = function(event) {
        onError(event)
    };

    webSocket.onopen = function(event) {
        onOpen(event)
    };

    webSocket.onmessage = function(event) {
        onMessage(event)
    };

    function onMessage(event) {
        document.getElementById('messages').innerHTML 
            += '<br />' + event.data;
    }

    function onOpen(event) {
        document.getElementById('messages').innerHTML 
            = 'Connection established';
    }

    function onError(event) {
        alert(event.data);
    }

    function start() {
        webSocket.send('hello');
        return false;
    }
</script>
</body>
</html>

<div id="消息"></div><script type="text/javascript">var webSocket =new WebSocket('ws://localhost:8080/com-byteslounge-websockets/websocket');webSocket.onerror = 函数(事件){onError(事件)};webSocket.onopen = 函数(事件){onOpen(事件)};webSocket.onmessage = 函数(事件){onMessage(事件)};函数 onMessage(事件){document.getElementById('messages').innerHTML+= '
'+ 事件数据;}函数 onOpen(事件){document.getElementById('messages').innerHTML= '连接建立';}函数 onError(event) {警报(事件.数据);}函数开始(){webSocket.send('你好');返回假;}</html>

Any ideas why no session is created ? Thanks

为什么没有创建会话的任何想法?谢谢

解决方案

推荐答案

这是预期的行为,但我同意它可能会令人困惑.来自 HandshakeRequest.getHttpSession javadoc:

/**
 * Return a reference to the HttpSession that the web socket handshake that 
 * started this conversation was part of, if the implementation
 * is part of a Java EE web container.
 *
 * @return the http session or {@code null} if either the websocket
 * implementation is not part of a Java EE web container, or there is
 * no HttpSession associated with the opening handshake request.
 */

问题是,尚未为您的客户端连接创建 HttpSession,而 WebSocket API 实现只是询问是否创建了某些内容,如果没有,它不创建它.您需要做的是调用 httpServletRequest.getSession() 某个时间 before WebSocket impl 过滤器被调用(doFilter(...) 被调用).

Problem is, that HttpSession was not yet created for your client connection and WebSocket API implementation just asks whether there is something created and if not, it does not create it. What you need to do is call httpServletRequest.getSession() sometime before WebSocket impl filter is invoked (doFilter(...) is called).

这可以通过例如在 ServletRequestListener#requestInitalized 或不同的过滤器等中调用提到的方法来实现.

This can be achieved for example by calling mentioned method in ServletRequestListener#requestInitalized or in different filter, etc..

这篇关于Websocket - httpSession 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆