我有带 LAMP 的 VPS。我有来自 Startssl.com 的免费签名 SSL 证书(SSL 证书工作正常)
使用 http://
协议(protocol)我可以连接到 ws://chat.example.com:1337/some-variable
但是当我替换协议(protocol) 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
最佳答案
为了更好地批评我的答案,因为最后一个是错误的,我本不想在完成挖掘之前提交它,试试这个:
// 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);
}
关于javascript - 无法连接到 wss://(建立连接时出错:net::ERR_CONNECTION_CLOSED),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34219104/