6

From WebSocket Endpoint I try to call Singleton Service. But I an unable to use Request or Session scope from WebSocket.

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

I get Scope 'request' is not active for the current thread; For 'request' or 'session' scope on any 'ScopedProxyMode'.

Thanks for the help!

Tonci Jelavic
  • 61
  • 1
  • 4

3 Answers3

1

For web sockets there are no request/response so the request scope is invalid. They introduced new scope called websocket in Spring 4.1. @Scope(name = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS). Example reference link

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

Abhinay
  • 464
  • 5
  • 13
  • 2
    While I'm aware of this new scope, it doesn't help retrieve data (e.g. currently logged in user) that already exists in the outside session scope. Any ideas how to access that? My client is sending the cookies that associate with my server session scope during the http handshake for its initial connection, but how do I find that scope from within Spring? – Periata Breatta Dec 22 '16 at 13:19
0

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

This is the immediate answer, however the better design approach would be to remove any reliance on sessions in the first place.

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33
0

You can use

@Scope(name = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)

But it is also singleton, not session scope. https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/websocket.html

David Buck
  • 3,752
  • 35
  • 31
  • 35
lmybill
  • 1
  • 1