0

I am testing Spring 4's different WebSocket approaches.

The most basic, works well, invoking the interceptor (I want to publish the HttpSession attributes):

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

Here is when the interceptor is invoked in org.springframework.web.socket.server.support.WebSocketHttpRequestHandler.handleRequest():

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

However when I use the SockJS handler, the interceptor is never called:

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

It seems that org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler, unlike WebSocketHttpRequestHandler, doesn't have a "interceptors" attribute.

Is this a Spring bug? Do you know any way to publish HttpSessionAttributes to SockJS websockets?

codependent
  • 23,193
  • 31
  • 166
  • 308

1 Answers1

2

Edited: 28 April, 2014:

Ok, what I have been doing is configuring Websockets through Java, as I found out that its more flexible:

Try this code as part of Java configuration:

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

I am using this code and its working for me. You would need to reference the spring-websocket documentation for the complete code.

Cheers

AliR
  • 2,065
  • 1
  • 27
  • 37
  • You can't define the sockjs element there (org.xml.sax.SAXParseException), the schema doesn't allow it. I think that the interceptor support isn't really implemented when using sockjs... – codependent Apr 25 '14 at 07:24