Why does sockJS add a '/info' to a given websocket url path

Lihkinisak picture Lihkinisak · Jan 28, 2016 · Viewed 7.6k times · Source

I want to open a websocket port with a "webapp/socket.do" path. When I use SockJS and try to initiate the call by code

    var socket = new SockJS('/webapp/socket.do');
    stompClient = Stomp.over(socket);

    stompClient.connect({}, ...

SockJS will by default add a "/info" to the end of the given path. I want to know why? Can this be changed or prevented?

When using this with Spring MVC and have url pattern mappings to DispatcherServlet like < url-pattern >*.do</url-pattern>, this will return a 404 error. it is blocked because of the "/info" string added by sockJS to the given url.

Spring web.xml servlet mapping code:

<servlet-mapping>
    <servlet-name>dispatch-servlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Does any know what sockJS is trying there and why?

Answer

Brian Clozel picture Brian Clozel · Jan 28, 2016

This is part of the SockJS protocol, and mandatory. This endpoint is implemented by the server and communicates the server capabilities, such as the supported protocols. See the relevant part of the SockJS protocol.

In that case, I guess you need to adapt your servlet mapping not only for this endpoint, but also for other requests that might come in: HTTP UPGRADE requests for websocket, all other requests for HTTP-based transports supported by SockJS.