Request or Session Area in Spring Websocket

From the WebSocket Endpoint I am trying to call the Singleton service. But I cannot use the request or session scope from WebSocket.

(@Scope (value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS))

I get the Request "Scope" inactive for the current thread; For the "request" or "session" area on any "ScopedProxyMode".

Thanks for the help!

+4
source share
2 answers

There is no request / response for web sockets, so the request scope is not valid. They introduced a new area called websocket in Spring 4.1. @Scope (name = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS). Link example

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#websocket-stomp-websocket-scope

+1
source

I'm not sure about Spring, but if you created your web socket code for the JSR-356 standard (which spring should understand), you can add a custom configurator to your endpoint definition where you can javax.websocket.server.ServerEndpointConfig.Configurator.modifyHandshake(ServerEndpointConfig, HandshakeRequest, HandshakeResponse) handshake. See javax.websocket.server.ServerEndpointConfig.Configurator.modifyHandshake(ServerEndpointConfig, HandshakeRequest, HandshakeResponse) , then you can access the actual session object, but it will depend on your application server. You can definitely get any parameters and headers.

This is an immediate answer, however the best design approach is to remove any session dependency first.

0
source

All Articles