我正在尝试使用以下命令在我的 Javascript (React) 应用程序中设置 WebSocketsSocket.io https://github.com/socketio/socket.io.
当使用polling
从 Socket.io 客户端(v1.4.5)传输,一切正常。
但我想使用实际且安全的 WebSockets (wss://
协议)...
我的服务器是 Debian 7,运行 Nginx v1.2.1 作为我的虚拟主机和 Node.js 应用程序的代理(使用 Socket.io v1.4.5)。
我的域名 (api.tribeez.net) 是通过以下方式设置的云耀光 https://www.cloudflare.com/, which 刚刚宣布 https://blog.cloudflare.com/everybody-gets-websockets/完整的WebSocketsupport https://support.cloudflare.com/hc/en-us/articles/200169466-Can-I-use-CloudFlare-with-WebSockets-(包括通过 SSL,即wss://
).
当我在 Chrome (v50.0) 中启动应用程序时,我在 Chrome 的网络面板中看到 WebSocket 连接(状态pending
)。
但一分钟后,WebSocket 断开连接(状态101 Switching Protocols
)控制台让我感到困惑WebSocket connection to 'wss://api.tribeez.net/socket.io/?EIO=3&transport=websocket&t=LI3t1dh' failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1
当打开socket.io调试时,这就是我得到的:
Fri, 06 May 2016 02:23:37 GMT socket.io:server incoming connection with id L43EOXmwsqkgCMVJAAAD
Fri, 06 May 2016 02:23:37 GMT socket.io:client connecting to namespace /
Fri, 06 May 2016 02:23:37 GMT socket.io:namespace adding socket to nsp /
Fri, 06 May 2016 02:23:37 GMT socket.io:socket socket connected - writing packet
Fri, 06 May 2016 02:23:37 GMT socket.io:socket joining room /#L43EOXmwsqkgCMVJAAAD
Fri, 06 May 2016 02:23:37 GMT socket.io:client writing packet {"type":0,"nsp":"/"}
Fri, 06 May 2016 02:23:37 GMT socket.io-parser encoding packet {"type":0,"nsp":"/"}
Fri, 06 May 2016 02:23:37 GMT socket.io-parser encoded {"type":0,"nsp":"/"} as 0
Transport=websocket, query=/socket.io/?EIO=3&transport=websocket&t=LI3t1dh
Fri, 06 May 2016 02:23:37 GMT socket.io:socket joined room /#L43EOXmwsqkgCMVJAAAD
Fri, 06 May 2016 02:23:37 GMT socket.io:socket joining room tribe#1
Fri, 06 May 2016 02:23:37 GMT socket.io:socket joined room tribe#1
Fri, 06 May 2016 02:24:37 GMT socket.io:client client close with reason transport close
Fri, 06 May 2016 02:24:37 GMT socket.io:socket closing socket - reason transport close
这是 Nginx 日志:
2016/05/06 04:24:37 [error] 28383#0: *549 upstream timed out (110: Connection timed out) while reading upstream, client: xxx.xxx.xxx.xxx, server: api.tribeez.net, request: "GET /socket.io/?EIO=3&transport=websocket&t=LI3t1dh HTTP/1.1", upstream: "http://127.0.0.1:3010/socket.io/?EIO=3&transport=websocket&t=LI3t1dh", host: "api.tribeez.net"
以及这个应用程序的 Nginx 配置:
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
}
server {
server_name api.tribeez.net;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:3010;
}
location /socket.io/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:3010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
知道为什么关闭吗?难道我需要定期 ping 一下吗?