首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJS和Primus.io WebSocket打开握手已取消

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

Stack Overflow用户
提问于 2014-11-30 22:05:46
回答 1查看 2.3K关注 0票数 0

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

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

我的NodeJS脚本:

代码语言:javascript
复制
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" });

客户端:

代码语言:javascript
复制
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反向代理之后。我无法访问that.So,这就是我使用wss的原因。

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

有人能帮帮忙吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2014-12-15 16:24:05

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

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

代码语言:javascript
复制
new Primus("http://dev.dylaan.nl", { websocket: false })

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27214090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档