Java 连接到 websocket 时我得到状态 200,但这是一个错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29829597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I get a status 200 when connecting to the websocket, but it is an error?
提问by smuggledPancakes
My error shows up in the console of my browser:
我的错误显示在浏览器的控制台中:
"WebSocket connection to 'ws://localhost:32768/DspClusterWebServices/myHandler' failed: Unexpected response code: 200"
"WebSocket connection to 'ws://localhost:32768/DspClusterWebServices/myHandler' failed: Unexpected response code: 200"
I am using Spring Websockets 4.1.5
and Tomcat 8.0.18
. My WebSocketConfigurer implementation class looks like:
我正在使用Spring Websockets 4.1.5
和Tomcat 8.0.18
。我的 WebSocketConfigurer 实现类如下所示:
@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();
}
}
My testWebsocketClient.js tries to connect with this code, but has a error code of 200:
我的 testWebsocketClient.js 尝试使用此代码连接,但错误代码为 200:
websocket = new WebSocket("ws://localhost:8080/myApp/myHandler");
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?
我不知道接下来要尝试什么。我认为这会导致 afterConnectionEstablished(WebSocketSession session) 方法触发?代码 200 不是很好吗?
采纳答案by xerx593
Please check http://procbits.com/connecting-to-a-sockjs-server-from-native-html5-websocket!
请检查http://procbits.com/connecting-to-a-sockjs-server-from-native-html5-websocket!
After you append /websocket
(to your URL), it will give you the error
在您追加/websocket
(到您的网址)后,它会给您错误
Failed to parse Origin header value [null]
无法解析 Origin 标头值 [null]
;) , which then will in turn lead you to that link.
;) ,然后将引导您到该链接。
You'll have to add .setAllowedOrigins("*")
to your addHandler()
method, and then it could finally work!
您必须添加.setAllowedOrigins("*")
到您的addHandler()
方法中,然后它终于可以工作了!
回答by Hannes
Have a look at the specification . The server should respond with 101
to signal protocol change from http
to ws
.
看看规格。服务器应响应101
从http
到 的信号协议更改ws
。
回答by Yang Peiyong
Please check that if 'ws://localhost:32768/DspClusterWebServices/myHandler' is correct.
请检查“ws://localhost:32768/DspClusterWebServices/myHandler”是否正确。
回答by Shuai Wang
For those guys like me who use angular + springboot and got this error. please check if you have enabled the redirect or forward all non api endpoint request back to index.html. like:
对于像我这样使用 angular + springboot 并出现此错误的人。请检查您是否已启用重定向或将所有非 api 端点请求转发回 index.html。喜欢:
@RequestMapping(value = "/**/{[path:[^\.]*}")
public String redirect() {
// Forward to home page so that route is preserved.
return "forward:/index.html";
}
If you do, disable it and you will get 101
如果你这样做,禁用它,你会得到 101
回答by Henry
Don't know if this is too late but a solution that I stumbled upon is simply appending the string /websocket
after the websocket endpoint that you declared in the spring boot server. This will help keep both the forwarding logic and connect and establish a websocket connection.
不知道这是否为时已晚,但我偶然发现的一个解决方案是简单地将字符串附加/websocket
到您在 Spring Boot 服务器中声明的 websocket 端点之后。这将有助于保持转发逻辑和连接并建立 websocket 连接。
回答by hatanooh
As my another answer:[https://stackoverflow.com/a/53272666/2930417][1]
作为我的另一个答案:[ https://stackoverflow.com/a/53272666/2930417][1]
I use springboot 2 +STOMP。
我用的是springboot 2 +STOMP。
remove .withSockJS()
,then everything is ok.
删除.withSockJS()
,然后一切正常。
I don't know the reason,but works for me.
我不知道原因,但对我有用。