简体   繁体   English

在WebSocketHandler中访问HTTP会话(Spring-websocket)

[英]Access HTTP Session in WebSocketHandler (Spring-websocket)

Dears, I'm trying to get an HTTPSession in my WebSocketHandler. 亲爱的,我正在尝试在我的WebSocketHandler中获取一个HTTPSession。 I could do it successfully when I was using 'javax.websocket-api' but I'm now using 'Spring-Websocket'. 当我使用'javax.websocket-api'时,我可以成功完成操作,但是现在使用的是'Spring-Websocket'。

The config : 配置:

@ConditionalOnWebApplication
@Configuration
@EnableWebSocket
public class WebSocketConfigurator implements WebSocketConfigurer {

    @Autowired
    private ApplicationContext context;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        MyEndpoint endpoint = context.getBean(MyEndpoint.class);
        registry.addHandler(endpoint, "/signaling");
    }
}

When the connection is established : 建立连接后:

    @Component
    public class MyEndpoint implements WebSocketHandler {

        private WebSocketSession wsSession;

        @Override
        public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
            this.wsSession = webSocketSession;
            // need to get the HTTP SESSION HERE        
            log.info("Opening: " + webSocketSession.getId());
        }
    }

And now this is an example of how I could do it using 'javax.websocket-api' : 现在,这是一个如何使用'javax.websocket-api'进行操作的示例:

The configuration : 配置:

@ServerEndpoint(value = "/signaling", //
        decoders = MessageDecoder.class, //
        encoders = MessageEncoder.class,
        configurator = MyEndpointConfigurator.class)
/***
 * define signaling endpoint         
 */
public class MyEndpoint extends NextRTCEndpoint {

}

Then I was Injecting the HTTPSession modifying the handshake : 然后,我注入了HTTPSession来修改握手:

public class MyEndpointConfigurator 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);
    }
}

And finally, it was accessible when the WS connection was established : 最后,当建立WS连接时可以访问它:

    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.wsSession = session;
        this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
        log.info("Opening: " + session.getId());

        server.register(session, httpSession);
    }

I could not succeed to do something similar with 'Spring Websocket'. 我无法成功完成与“ Spring Websocket”类似的操作。 Any solution ? 有什么办法吗? Please do not propose classes from StompJS as I'm not using it. 请不要从StompJS提出类,因为我没有使用它。

There is this one to use: 有一个可以使用的:

**
 * An interceptor to copy information from the HTTP session to the "handshake
 * attributes" map to made available via{@link WebSocketSession#getAttributes()}.
 *
 * <p>Copies a subset or all HTTP session attributes and/or the HTTP session id
 * under the key {@link #HTTP_SESSION_ID_ATTR_NAME}.
 *
 * @author Rossen Stoyanchev
 * @since 4.0
 */
public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor {

And there is a sample in the Reference Manual how to configure it: 参考手册中有一个示例如何配置它:

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(new MyHandler(), "/myHandler")
        .addInterceptors(new HttpSessionHandshakeInterceptor());
}

So, whatever you need from the HTTP session is going to be available in the WebSocketSession.getAttributes() . 因此,无论HTTP会话需要什么,都将在WebSocketSession.getAttributes()可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM