NodeJS和Primus.io WebSocket打开握手被取消

时间:2014-11-30 14:05:46

标签: node.js proxy websocket primus

我正在尝试使用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

有人可以帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

在我看来,您已经错误地配置了nginx服务器并且它阻止了WebSocket连接,导致返回400错误。请参阅http://nginx.com/blog/websocket-nginx/,了解如何配置服务器以允许websocket连接。

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

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

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