我有带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
为了更好地批评我的答案,因为上一个是错误的,我本打算在挖掘完成后才提交,请尝试以下操作:
// 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);
}