我正在学习使用Spring WebSockets,但我无法获得一个非常基本的示例,我收到404错误。 我使用http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html作为参考。
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();
}
}
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!");
}
}
除非我犯了错误,否则我应该能够通过websocket从客户端向服务器发送消息,如下所示:
sock = new WebSocket('ws://localhost:8080/StreamGame/ws/play');
我在连接时收到以下错误:
WebSocket握手期间出错:意外的响应代码:404WrappedWebSocket @ VM84:35(匿名函数)@(索引):11j @jquery-3.1.1.min.js:2k @jquery-3.1.1.min.js: 2 (索引):26错误未定义
Web.xml包含以下内容:
<!-- 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应用程序上下文包含:
连接websocket时,我在服务器日志中看到以下内容:
> 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'
答案 0 :(得分:1)
您的websocket网址似乎不正确。您已将 / play 注册为处理程序,但请致电 / StreamGame / play
还要记住,ws连接是异步创建的,所以你必须等待连接握手完成才能发送消息,如下所示:
var ws = new WebSocket("ws://localhost:8080/play");
ws.onopen = function() {
ws.send('AHOIHOI');
}
ws.onerror = function(error) {
// handle connection error
}
答案 1 :(得分:0)
addHandler函数需要映射到&#39; / play&#39;,因此不包括根上下文(StreamGame)和调度程序映射(/ ws)。更改addHandler解决了这个问题:
registry.addHandler(gameWebSocketHandler(), "/play");
打开时连接的websocket如下:
sock = new WebSocket('ws://localhost:8080/StreamGame/ws/play');