Websocket - httpSession zwraca wartość null
Chciałbym nawiązać połączenie między sesją uzgadniania websocket a obiektem HttpSession.
Użyłem następującej modyfikacji uzgadniania:
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);
}
}
Jak wspomniano w tym poście:Dostęp do HttpSession z HttpServletRequest w Web Socket @ServerEndpoint
Teraz, z jakiegoś powodu na drżenie ręki, request.getHttpSession () zwraca (null) cały czas.
tutaj jest mój kod po stronie klienta:
<!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>
Jakieś pomysły, dlaczego nie tworzono sesji? Dzięki