Request or Session scope in Spring Websocket

℡╲_俬逩灬. 提交于 2019-12-23 12:13:08

问题


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!


回答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




回答2:


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.



来源:https://stackoverflow.com/questions/31916727/request-or-session-scope-in-spring-websocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!