問題:
webscoket.js:26 WebSocket connection to ‘ws://localhost:8088/DreamWeb/ctrl/websocketServer.do?userId=180627211146gYHGYcQlcBFRqW8nRUC’ failed: Error during WebSocket handshake: Unexpected response code: 403
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL1QTNwEjN1AjMxAzMwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
問題分析:
一,WebSocket 設定為不允許跨域。
二,伺服器/代理伺服器不支援WebSocket
解決方案
Java
在websocket連接配接注冊時設定允許跨域
伺服器設定(以Nginx為例 )
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
ip_hash;
server 127.0.0.1:9090 weight=2;
server 127.0.0.1:9091 weight=2;
}
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Origin '';
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_connect_timeout 7200;
proxy_read_timeout 3600;
proxy_send_timeout 3600;
}
}