WebSocket 连接失败:一个或多个保留位打开:reserved1 = 0,reserved2 = 1,reserved3 = 1
WebSocket connection failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1
我正在尝试使用 Socket.io.
在我的 Javascript (React) 应用程序中设置 WebSockets
从 Socket.io 客户端 (v1.4.5) 使用 polling
传输时,一切正常。
但我想使用实际和安全的 WebSockets(wss://
协议)...
我的服务器是 Debian 7 运行 Nginx v1.2.1 作为我的虚拟主机和我的 Node.js 应用程序(使用 Socket.io v1.4.5)的代理。
我的域名 (api.tribeez.net) 是通过 CloudFlare, which just announced full WebSocket support 设置的(包括通过 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 conf:
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一下?
在 Nginx blogpost: I needed to upgrade my Nginx version to a 1.3.13+. I installed the 1.10 (latest stable) using the right Nginx Debian repository (see instructions) 的底部找到了解决问题的答案。
这是设置 localStorage.debug = '*'
后我的浏览器输出:
socket.io-client:url parse https://api.tribeez.net +0ms
socket.io-client new io instance for https://api.tribeez.net +5ms
socket.io-client:manager readyState closed +2ms
socket.io-client:manager opening https://api.tribeez.net +1ms
engine.io-client:socket creating transport "polling" +2ms
engine.io-client:polling polling +2ms
engine.io-client:polling-xhr xhr poll +1ms
engine.io-client:polling-xhr xhr open GET: https://api.tribeez.net/socket.io/?EIO=3&transport=polling&t=LI4PaE9 +2ms
engine.io-client:polling-xhr xhr data null +2ms
engine.io-client:socket setting transport polling +1ms
socket.io-client:manager connect attempt will timeout after 20000 +3ms
socket.io-client:manager readyState opening +3ms
engine.io-client:polling polling got data ArrayBuffer +468ms
engine.io-client:socket socket receive: type "open", data "{"sid":"0BCyhfQm--8uOQYUAAAb","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +5ms
engine.io-client:socket socket open +1ms
socket.io-client:manager open +0ms
socket.io-client:manager cleanup +1ms
socket.io-client:socket transport is open - connecting +0ms
engine.io-client:socket starting upgrade probes +1ms
engine.io-client:socket probing transport "websocket" +1ms
engine.io-client:socket creating transport "websocket" +0ms
engine.io-client:polling polling +2ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://api.tribeez.net/socket.io/?EIO=3&transport=polling&t=LI4PaLo&sid=0BCyhfQm--8uOQYUAAAb +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:polling polling got data ArrayBuffer +252ms
engine.io-client:socket socket receive: type "message", data "0" +1ms
socket.io-parser decoded 0 as {"type":0,"nsp":"/"} +0ms
engine.io-client:polling polling +2ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://api.tribeez.net/socket.io/?EIO=3&transport=polling&t=LI4PaPo&sid=0BCyhfQm--8uOQYUAAAb +0ms
engine.io-client:polling-xhr xhr data null +1ms
engine.io-client:socket probe transport "websocket" opened +441ms
engine.io-client:socket probe transport "websocket" pong +252ms
engine.io-client:socket pausing current transport "polling" +1ms
engine.io-client:polling we are currently polling - waiting to pause +1ms
engine.io-client:polling polling got data ArrayBuffer +98ms
engine.io-client:socket socket receive: type "noop", data "undefined" +1ms
engine.io-client:polling pre-pause polling complete +0ms
engine.io-client:polling paused +1ms
engine.io-client:socket changing transport and sending upgrade packet +0ms
engine.io-client:socket setting transport websocket +0ms
engine.io-client:socket clearing existing transport polling +1ms
engine.io-client:polling ignoring poll - transport state "paused" +0ms
Socket.io 似乎无论如何都在打开一个轮询连接,但是一旦 websocket 工作就暂停它以更喜欢 websocket(我想......)
我正在尝试使用 Socket.io.
在我的 Javascript (React) 应用程序中设置 WebSockets从 Socket.io 客户端 (v1.4.5) 使用 polling
传输时,一切正常。
但我想使用实际和安全的 WebSockets(wss://
协议)...
我的服务器是 Debian 7 运行 Nginx v1.2.1 作为我的虚拟主机和我的 Node.js 应用程序(使用 Socket.io v1.4.5)的代理。
我的域名 (api.tribeez.net) 是通过 CloudFlare, which just announced full WebSocket support 设置的(包括通过 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 conf:
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一下?
在 Nginx blogpost: I needed to upgrade my Nginx version to a 1.3.13+. I installed the 1.10 (latest stable) using the right Nginx Debian repository (see instructions) 的底部找到了解决问题的答案。
这是设置 localStorage.debug = '*'
后我的浏览器输出:
socket.io-client:url parse https://api.tribeez.net +0ms
socket.io-client new io instance for https://api.tribeez.net +5ms
socket.io-client:manager readyState closed +2ms
socket.io-client:manager opening https://api.tribeez.net +1ms
engine.io-client:socket creating transport "polling" +2ms
engine.io-client:polling polling +2ms
engine.io-client:polling-xhr xhr poll +1ms
engine.io-client:polling-xhr xhr open GET: https://api.tribeez.net/socket.io/?EIO=3&transport=polling&t=LI4PaE9 +2ms
engine.io-client:polling-xhr xhr data null +2ms
engine.io-client:socket setting transport polling +1ms
socket.io-client:manager connect attempt will timeout after 20000 +3ms
socket.io-client:manager readyState opening +3ms
engine.io-client:polling polling got data ArrayBuffer +468ms
engine.io-client:socket socket receive: type "open", data "{"sid":"0BCyhfQm--8uOQYUAAAb","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +5ms
engine.io-client:socket socket open +1ms
socket.io-client:manager open +0ms
socket.io-client:manager cleanup +1ms
socket.io-client:socket transport is open - connecting +0ms
engine.io-client:socket starting upgrade probes +1ms
engine.io-client:socket probing transport "websocket" +1ms
engine.io-client:socket creating transport "websocket" +0ms
engine.io-client:polling polling +2ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://api.tribeez.net/socket.io/?EIO=3&transport=polling&t=LI4PaLo&sid=0BCyhfQm--8uOQYUAAAb +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:polling polling got data ArrayBuffer +252ms
engine.io-client:socket socket receive: type "message", data "0" +1ms
socket.io-parser decoded 0 as {"type":0,"nsp":"/"} +0ms
engine.io-client:polling polling +2ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://api.tribeez.net/socket.io/?EIO=3&transport=polling&t=LI4PaPo&sid=0BCyhfQm--8uOQYUAAAb +0ms
engine.io-client:polling-xhr xhr data null +1ms
engine.io-client:socket probe transport "websocket" opened +441ms
engine.io-client:socket probe transport "websocket" pong +252ms
engine.io-client:socket pausing current transport "polling" +1ms
engine.io-client:polling we are currently polling - waiting to pause +1ms
engine.io-client:polling polling got data ArrayBuffer +98ms
engine.io-client:socket socket receive: type "noop", data "undefined" +1ms
engine.io-client:polling pre-pause polling complete +0ms
engine.io-client:polling paused +1ms
engine.io-client:socket changing transport and sending upgrade packet +0ms
engine.io-client:socket setting transport websocket +0ms
engine.io-client:socket clearing existing transport polling +1ms
engine.io-client:polling ignoring poll - transport state "paused" +0ms
Socket.io 似乎无论如何都在打开一个轮询连接,但是一旦 websocket 工作就暂停它以更喜欢 websocket(我想......)