0

我正在测试 Spring 4 的不同 WebSocket 方法。

最基本的,运行良好,调用拦截器(我想发布 HttpSession 属性):

<websocket:handlers>
    <websocket:mapping path="/simpleChatHandler" handler="simpleChatHandler"/>
    <websocket:handshake-interceptors>
        <bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
    </websocket:handshake-interceptors>
</websocket:handlers>

这是在调用拦截器时org.springframework.web.socket.server.support.WebSocketHttpRequestHandler.handleRequest()

[...]
try {
    Map<String, Object> attributes = new HashMap<String, Object>();
    if (!chain.applyBeforeHandshake(request, response, attributes)) {

但是,当我使用 SockJS 处理程序时,永远不会调用拦截器:

<websocket:handlers>
    <websocket:mapping path="/simpleChatHandlerSockJS" handler="simpleChatHandler"/>
    <websocket:handshake-interceptors>
        <bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
    </websocket:handshake-interceptors>
    <websocket:sockjs/>
</websocket:handlers>

org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler与 不同,它似乎WebSocketHttpRequestHandler没有“拦截器”属性。

这是春天的错误吗?您知道将 HttpSessionAttributes 发布到 SockJS websockets 的任何方法吗?

4

1 回答 1

2

编辑:2014 年 4 月 28 日:

好的,我一直在做的是通过 Java 配置 Websockets,因为我发现它更灵活:

尝试将此代码作为 Java 配置的一部分:

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
  registry.addHandler(new Handler(), "/endpoint")
    .addInterceptors(new HttpSessionHandshakeInterceptors())
  .withSockJS();
}

我正在使用此代码及其为我工作。您需要参考 spring-websocket 文档以获取完整代码。

干杯

于 2014-04-24T04:41:12.140 回答