Request or Session scope in Spring Websocket

2019-06-22 06:58发布

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!

2条回答
smile是对你的礼貌
2楼-- · 2019-06-22 07:37

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

查看更多
干净又极端
3楼-- · 2019-06-22 07:50

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.

查看更多
登录 后发表回答