我正在學習使用Spring WebSockets,但我無法獲得一個非常基本的示例來工作,我收到一個404錯誤。 我用http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html作爲參考。非常基本的Spring websocket返回404
的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>
我Spring應用程序上下文包含:
當連接的WebSocket我看到在服務器日誌如下:
> 2016-11-13 TRACE - 39804 - org.springframework.web.servlet.DispatcherServlet -
> http-nio-8080-exec-2 - Bound request context to thread:
> [email protected]
> 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.sp[email protected]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'
嗨, 謝謝您的建議。我曾嘗試將websocket配置更改爲/ StreamGame/play,但結果相同: registry.addHandler(gameWebSocketHandler(),「/ StreamGame/play」); – Wesley
好的,如果你的類沒有實例化,那麼它就無法工作。你使用Spring-Boot來設置應用程序嗎?如果不是,您可能會忘記正確設置組件掃描類路徑。 – Quagaar
嗨Quagaar, 我添加了我的application-context.xml和web.xml到我原來的文章。我是否需要在web.xml中定義更多內容? – Wesley