连接到websocket时我得到状态200,但这是一个错误? [英] I get a status 200 when connecting to the websocket, but it is an error?
问题描述
我的错误显示在浏览器的控制台中:
My error shows up in the console of my browser:
WebSocket连接到'ws:// localhost:32768 / DspClusterWebServices / myHandler'失败:意外的响应代码:200
我正在使用 Spring Websockets 4.1.5
和 Tomcat 8.0.18
。我的WebSocketConfigurer实现类如下所示:
I am using Spring Websockets 4.1.5
and Tomcat 8.0.18
. My WebSocketConfigurer implementation class looks like:
@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:
My testWebsocketClient.js tries to connect with this code, but has a error code of 200:
websocket = new WebSocket("ws://localhost:8080/myApp/myHandler");
我无法弄清楚下一步该尝试什么。我以为这会导致afterConnectionEstablished(WebSocketSession会话)方法被触发?代码200不好吗?
I cannot figure out what to try next. I thought that this would cause the afterConnectionEstablished(WebSocketSession session) method to fire? Isn't code 200 good?
推荐答案
请检查 http://procbits.com/connecting-to-a-sockjs-server-from-native-html5-websocket !
在您附加 / websocket
(到您的网址)后,它会给您错误
After you append /websocket
(to your URL), it will give you the error
无法解析Origin标头值[null]
Failed to parse Origin header value [null]
;)
,然后将引导您该链接。
您必须将 .setAllowedOrigins(*)
添加到 addHandler()
方法,然后它终于可以工作了!
You'll have to add .setAllowedOrigins("*")
to your addHandler()
method, and then it could finally work!
这篇关于连接到websocket时我得到状态200,但这是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!