Wesley Wesley - 17 days ago 9
Java Question

Very basic Spring websocket returns 404

I am learning to use Spring WebSockets, but I cannot get a very basic example to work, I receive a 404 error.
I used http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html as a reference.

The WebSocketConfig:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
private Logger logger = Logger.getLogger(WebSocketConfig.class);

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
logger.debug("Registering websocket handler");
registry.addHandler(gameWebSocketHandler(), "/StreamGame/ws/play");
}

@Bean
public WebSocketHandler gameWebSocketHandler() {
return new GameWebSocketHandler();
}
}


The WebSocketHandler:

public class GameWebSocketHandler extends TextWebSocketHandler {
final static Logger logger = Logger.getLogger(GameWebSocketHandler.class);

public GameWebSocketHandler() {
logger.debug("Constructor handler");
}

@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
super.handleTransportError(session, exception);
logger.error("Transport error: " + exception.getMessage());
}

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
logger.info("Handle message received: " + message.getPayload());
//session.sendMessage(new TextMessage("Here is your answer"));
}

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
super.afterConnectionEstablished(session);
logger.info("Connection established!");
}
}


Unless I am mistaking, even with this I should be able to send a message through a websocket from the client to the server as following:

sock = new WebSocket('ws://localhost:8080/StreamGame/ws/play');


I receive the following error on connect:


Error during WebSocket handshake: Unexpected response code: 404WrappedWebSocket @ VM84:35(anonymous function) @ (index):11j @ jquery-3.1.1.min.js:2k @ jquery-3.1.1.min.js:2
(index):26 Error undefined


Web.xml contains the following:

<!-- Processes application requests -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Mapping websocket to spring dispatcher servlet -->
<servlet-mapping>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>


My Spring application context contains:




When connecting the websocket I see the following in the server logs:

> 2016-11-13 TRACE - 39804 - org.springframework.web.servlet.DispatcherServlet -
> http-nio-8080-exec-2 - Bound request context to thread:
> org.apache.catalina.connector.RequestFacade@df5f117
> 2016-11-13 DEBUG - 39804 - org.springframework.web.servlet.DispatcherServlet -
> http-nio-8080-exec-2 - DispatcherServlet with name
> 'SpringMVCDispatcherServlet' processing GET request for
> [/StreamGame/ws/play]
> 2016-11-13 TRACE - 39804 - org.springframework.web.servlet.DispatcherServlet -
> http-nio-8080-exec-2 - Testing handler map
> [org.springframework.web.socket.server.support.WebSocketHandlerMapping@4e92ebf3]
> in DispatcherServlet with name 'SpringMVCDispatcherServlet'
> 2016-11-13 TRACE - 39804 - org.springframework.web.socket.server.support.WebSocketHandlerMapping
> - http-nio-8080-exec-2 - No handler mapping found for [/play]
> 2016-11-13 WARN - 39804 - org.springframework.web.servlet.PageNotFound - http-nio-8080-exec-2 -
> No mapping found for HTTP request with URI [/StreamGame/ws/play] in
> DispatcherServlet with name 'SpringMVCDispatcherServlet'

Answer

The addHandler function needs to be mapped to '/play', so excluding the root-context (StreamGame) and the dispatcher mapping (/ws). Changing the addHandler solved the issue:

registry.addHandler(gameWebSocketHandler(), "/play");

The websocket connected when opening it as following:

sock = new WebSocket('ws://localhost:8080/StreamGame/ws/play');
Comments