WebSocket 连接失败:一个或多个保留位打开:reserved1 = 0、reserved2 = 1、reserved3 = 1

2024-06-25

我正在尝试使用以下命令在我的 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 一下吗?


在底部找到了答案Nginx 博客文章 https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/:我需要将 Nginx 版本升级到 1.3.13+。我使用正确的 Nginx Debian 存储库安装了 1.10(最新稳定版本)(参见说明 http://nginx.org/en/linux_packages.html),这解决了这个问题。

这是我设置后的浏览器输出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 (我想......)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WebSocket 连接失败:一个或多个保留位打开:reserved1 = 0、reserved2 = 1、reserved3 = 1 的相关文章

  • 使用 Sequelize.js 和 PostgreSQL 查询关联模型上的 JSONB 字段

    我有我的两个模型Foo and Bar Foo有一个字段barId 因此有一个Bar与其关联的对象 我可以查询我所有的Foo对象并包括它们的关联Bar对象如此 我正在使用 TypeScript续集打字稿 https github com R
  • 每次页面重新加载时都会出现新的 Google 登录库提示

    在我的项目中 我使用常规弹出客户端 JS 身份验证 平台客户端 https developers google com identity sign in web sign in 我从旧版 Google Sign 迁移到新版 Google I
  • 如何处理node.js中的for循环?

    我在 node js 中有以下代码 var months jan feb march april may june july august sep oct nov dec for var i 0 j months length i
  • nginx 服务器配置:子域到文件夹

    我从 Apache2 迁移到 nginx 在处理子域控制时遇到问题 我想要什么 当请求 x domain tld 时 内部重写为domain tld x 我遇到的问题是 nginx 总是通过告诉浏览器重定向到来重定向页面 但我真正想要的是在
  • 简单的node js应用程序

    我正在尝试制作一个简单的表单 其中包含用户名和姓氏 当用户提交信息时 会显示另一个页面 我用 html 做了一个表单 但我不知道下一步该怎么做 有没有人有一个小型的 独立的表单示例 使用node js 这个例子并没有完全完成你的任务 但它是
  • NodeJS 需要全局模块/包

    我正在尝试全局安装然后使用forever and forever monitor像这样 npm install g forever forever monitor 我看到了通常的输出以及将文件复制到全局路径的操作 但是如果我尝试requir
  • 咕噜玉错误

    每当我运行 grunt james 时 我都会收到错误 Warning pattern indexOf is not a function Use force to continue 现在这是我的玉石任务 jade options pret
  • 页面路由不完全是我想要的

    我在下面的代码中使用 Angular Routing 将用户引导至新的注册表单 该应用程序的工作原理如下 用户看到一个网页 要求他们通过单击适当的按钮在 数字报价 或 屏幕报价 之间做出选择 单击该按钮后 它应该将其定向到另一个显示相应注册
  • 部署到 azure 网站时如何 npm 配置选项(例如 --scripts-prepend-node-path)?

    部署到 azure 网站时 我在 package json 中使用了以下内容 name mypackage engines node 6 11 1 npm 4 2 0 一切正常 但我也收到以下警告 npm WARN 生命周期 用于脚本的节点
  • 在同一个 fd 上多次 createReadStream

    在上一个问题中 https stackoverflow com questions 13932967似乎从 node js 中的文件进行随机访问读取的唯一方法是使用fs createReadStream http nodejs org ap
  • 将 React 应用程序作为后台进程运行

    我对部署前端代码完全陌生 因此提出了这个问题 我有一个 React 应用程序 需要作为后台进程运行 但是我对如何执行此操作有点困惑 我运行一个 npm 脚本 npm run build 在服务器上构建 缩小和服务项目 构建过程的相关代码是这
  • 玩笑 toEqual 不适用于异常匹配

    我是新来的jest所以我不确定是否是我的错 这是我的测试用例 it should throw error if wrong email or phone number is provided async gt await expect us
  • Puppeteer - 错误:协议错误 (Network.getResponseBody):找不到具有给定标识符的资源

    我正在尝试使用此代码使用 puppeteer 从网站获取响应正文 usr bin env node require dotenv config const puppeteer require puppeteer const readline
  • Node.js:301 重定向非 www,不带 Express

    对于现有项目 我只想做一个简单的更改 将 www mysite com 重定向到 mysite com 因为 cookie 问题 非 www 版本无法访问 www 上的 cookie 我不想包括快递 我该如何做这个简单的改变 我认为这就是您
  • 防止 Node.js 中的 SQL 注入

    是否有可能以与 PHP 具有防范 SQL 注入的预准备语句相同的方式防止 Node js 中的 SQL 注入 最好使用模块 如果是这样 怎么办 如果不 有哪些例子这可能会绕过我提供的代码 见下文 一些背景 我正在制作一个 Web 应用程序
  • 如果数据库关闭一段时间,MongoDB 会更改流超时

    我在nodejs中使用mongoDB更改流 一切正常 但如果数据库关闭需要超过10 5秒才能启动更改流会引发超时错误 这是我的更改流观察程序代码 Service prototype watcher function db let colle
  • 如何在 React Native 应用程序中的 iOS 后台运行 socket.io?

    我在用socket io in an iOS React Native v0 20 app 该应用程序会跟踪我的位置 当我的位置发生变化时 它会向服务器发送一条消息 如果套接字连接丢失 服务器会发送电子邮件通知我 我在后台进行位置跟踪反应本
  • 脚本不会从nodejs应用程序中的ejs文件运行

    我正在尝试使用nodejs express mysql和ejs让网页显示图表 但我显然不明白ejs javascript等是如何工作的 我需要运行一个脚本来设置图表 来自 Chart js 模块 但它不会输出任何类型的图表 我尝试过的 将
  • 如何阻止我的 App Engine 应用程序的“appspot.com”?

    我添加了一个自定义域 并将其添加到 App Engine 例如 假设我的自定义域是 example com 我的应用程序由 Node js 提供服务 当我通过 App Engine 部署我的应用程序时 它会向我提供以下地址来访问它 exam
  • 在 OpenShift 上部署 Create React App:EACCES:权限被拒绝,打开 '/home/node/app/.eslintcache'

    我正在尝试使用 Dockerfile 在 OpenShift 上部署 Create React App webapp OpenShift 构建成功完成 当我访问路线时 我可以看到应用程序运行 1 秒 然后屏幕上出现此错误 Failed to

随机推荐