无法连接到 wss://(连接建立错误:net::ERR_CONNECTION_CLOSED)
Posted
技术标签:
【中文标题】无法连接到 wss://(连接建立错误:net::ERR_CONNECTION_CLOSED)【英文标题】:Can't connect to wss:// (Error in connection establishment: net::ERR_CONNECTION_CLOSED) 【发布时间】:2015-12-11 08:30:35 【问题描述】:我有带 LAMP 的 VPS。我有来自 Startssl.com 的免费签名 SSL 证书(ssl 证书正常工作)
使用http://
协议,我可以连接到ws://chat.example.com:1337/some-variable
,但是当我将协议http://
替换为https://
时,我无法连接到wss://chat.example.com:1337/some-variable
当我尝试连接到 wss://
时,我收到错误 Error in connection establishment: net::ERR_CONNECTION_CLOSED
为什么?
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
"use strict";
// Optional. You will see this name in eg. 'ps' or 'top' command
process.title = 'node-chat';
// Port where we'll run the websocket server
var webSocketsServerPort = 1337;
// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');
var $ = require("jquery");
/**
* HTTP server
*/
var server = http.createServer(function(request, response)
// Not important for us. We're writing WebSocket server, not HTTP server
);
server.listen(webSocketsServerPort, function()
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
);
/**
* WebSocket server
*/
var wsServer = new webSocketServer(
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
);
编辑
我有来自http://ahoj.io/nodejs-and-websocket-simple-chat-tutorial的来源
【问题讨论】:
【参考方案1】:为了更好地批评我的答案,因为最后一个是错误的,我不想在我完成挖掘之后提交它,试试这个:
// Private key and certification
var options =
key: fs.readFileSync('cert/server.key'),
cert: fs.readFileSync('cert/server.crt')
;
var server = https.createServer(options, function(request, response)
console.log((new Date()) + ' Received HTTP(S) request for ' + request.url);
【讨论】:
cert/server.key
可以保存在etc/apache2/myssl/server.key
吗?或者必须在var/www/html/
?
没问题。我为第一个答案道歉 - 从来没有打算提交它。如果您愿意,可以将文本字段用作“记事本”,而我正在查看它。 :)以上是关于无法连接到 wss://(连接建立错误:net::ERR_CONNECTION_CLOSED)的主要内容,如果未能解决你的问题,请参考以下文章
WebSocket 连接到“wss://argus.chi.shopify.io”失败:WebSocket 在连接建立之前关闭
失败:连接建立错误:net::ERR_CONNECTION_REFUSED