我的错误显示在浏览器的控制台中:
"WebSocket connection to 'ws://localhost:32768/DspClusterWebServices/myHandler' failed: Unexpected response code: 200"
我正在使用Spring Websockets 4.1.5和Tomcat 8.0.18。我的WebSocketConfigurer实现类如下所示:
Spring Websockets 4.1.5
Tomcat 8.0.18
@Configuration @Controller @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { class MyHandler implements WebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { System.out.println("afterConntectionEstablished called"); } ...implements rest of functions with a System.out.println and false for supportsPartialMessages() } } @Override registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "myHandler").withSockJS(); } @Bean public WebSocketHandler myHandler() { return new MyHandler(); } }
我的testWebsocketClient.js尝试与此代码连接,但错误代码为200:
websocket = new WebSocket("ws://localhost:8080/myApp/myHandler");
我不知道下一步该怎么做。我以为这会导致afterConnectionFounded(WebSocketSession session)方法启动?代码200不好吗?
请检查http://procbits.com/connecting-to-a-sockjs-server-from-native- html5-websocket!
附加/websocket(到您的URL)后,它将给您错误
/websocket
无法解析Origin标头值[null]
;),然后依次将您带到该链接。
您必须添加.setAllowedOrigins("*")到您的addHandler()方法中,然后它才可以正常工作!
.setAllowedOrigins("*")
addHandler()