Websocket - httpSession返回null

Urb*_*leg 13 session tomcat httpsession websocket

我想在websocket handshake\session与HttpSession对象之间建立连接.

我使用了以下握手修改:

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);
}
}
Run Code Online (Sandbox Code Playgroud)

正如本文所述: 在Web Socket @ServerEndpoint中从HttpServletRequest访问HttpSession

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

这是我的客户端代码:

<!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>
Run Code Online (Sandbox Code Playgroud)

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

Pav*_*cek 10

这是预期的行为,但我同意这可能令人困惑.来自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.
 */
Run Code Online (Sandbox Code Playgroud)

问题是,尚未为您的客户端连接创建HttpSession,并且WebSocket API实现只询问是否创建了某些内容,如果没有,则不会创建它.你需要做的是调用httpServletRequest.getSession()某个之前被调用的WebSocket实现了一套过滤器(doFilter(...)被调用).

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

  • 如何从传递给requestInitalized方法的ServletRequestEvent pServletRequestEvent对象中获取httpservletRequest对象? (3认同)

wut*_*aer 8

这是Pavel Bucek的答案的一个impl,在添加之后,我得到了我的会话

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

@WebListener
public class RequestListener implements ServletRequestListener {

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        // TODO Auto-generated method stub

    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        ((HttpServletRequest) sre.getServletRequest()).getSession();
    }

}
Run Code Online (Sandbox Code Playgroud)