【问题标题】:NodeJS and Primus.io WebSocket opening handshake was canceledNodeJS 和 Primus.io WebSocket 打开握手被取消
【发布时间】:2015-01-28 14:44:27
【问题描述】:

我正在尝试使用 websockets 与 Primus 连接:

WebSocket connection to 'wss://dev.dylaan.nl/primus/?_primuscb=1417355606238-6' failed: WebSocket opening handshake was canceled

我的 NodeJS 脚本:

var express     = require("express")
,   fs          = require("fs")
,   path        = require("path")
,   moment      = require("moment")
,   Primus      = require("primus")
,   compression = require("compression")
,   app         = express()
,   http        = require("http").createServer(app)
,   jsp         = require("uglify-js").parser
,   pro         = require("uglify-js").uglify
,   primus      = new Primus(http, { transformer: "websockets" });

客户:

var primus      = new Primus("wss://dev.dylaan.nl")

我是这种东西的新手,但我现在不知道出了什么问题,我已经阅读了 Primus 的文档,我也尝试过谷歌但我可以找到答案。

当我将协议更改为 ws 而不是 wss 时,我得到了错误

Error during WebSocket handshake: Unexpected response code: 426

http 426 may mean that you are trying to connect with an unsupported websocket protocol version

Also, if you are connecting through a proxy, the proxy may remove the "upgrade" header from the request since it is marked as "connection" header. Switch to WSS:// to prevent that.

@ Unexpected response code: 426 with PhanthomJS Websocket client/ROSLIB

嗯,我的 VPS 是在一个 ngnix 反向代理后面。我无法访问它。所以这就是我使用 wss 的原因。

演示:http://dev.dylaan.nl/play

有人可以帮忙吗?

谢谢!

【问题讨论】:

    标签: node.js proxy websocket primus


    【解决方案1】:

    在我看来,您的 nginx 服务器配置不正确,它阻塞了 WebSocket 连接,导致返回 400 错误。请参阅http://nginx.com/blog/websocket-nginx/,了解如何配置服务器以允许 websocket 连接。

    如果您无法让您的主机/vps 验证是否允许 WebSocket 连接,您可以通过在客户端代码中提供选项 { websocket: false } 在 Primus 中完全关闭 WebSocket:

    new Primus("http://dev.dylaan.nl", { websocket: false })
    

    我还注意到您使用 wss/ws 而不是 Primus 要求的 http,我们会自动将基于 HTTP 的 url 重写为正确的格式。

    【讨论】:

    • 我使用的是 Primus 2.4.10 版本,客户端库接受 ws:// url。
    猜你喜欢
    • 2017-03-19
    • 2023-03-08
    • 1970-01-01
    • 2019-12-22
    • 2023-03-05
    • 1970-01-01
    • 2019-11-04
    • 2011-04-07
    • 1970-01-01
    相关资源
    最近更新 更多