Error during WebSocket handshake: Unexpected response code: 200
解决方案:
1、https的需要使用 wss://
2、(非nginx跳过)nginx代理需要配置持续连接。
这里还要注意一下,如果你连接服务器携带了参数,如 /ws/{id} ,下面这个配置也是同样生效的!不需要另外获取这个 {id},再转发到 127.0.0.1/ws/{id}。还有可能出现的问题是:这个 @ServerEndpoint(value = "/ws/{id}") 中的 /ws/id 被另外一个 RequestMapping 使用了,程序中是不会报错的,但是在 nginx 中的配置是不行的,默认会转发到 RequestMapping 的路径,所以如下配置在这个情况是不生效的。
location /ws {
proxy_pass http://127.0.0.1:5011/ws/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
3、在系统中使用了权限框架,没有把 Websocket 的连接地址放出来。
4、(非Stomp+Websocket跳过)在继承 WebConfigurer 的类中注册拦截器时,多加了 .withSockJS() 方法,删除如下的 .withSockJS() 方法。