无法连接到wss://(连接建立错误:net :: ERR_CONNECTION_CLOSED)

时间:2015-12-11 08:30:35

标签: javascript ssl websocket

我有LPS的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 个答案:

答案 0 :(得分:0)

更好地批评我的答案,因为最后一个是错误的,我并不意味着提交它直到我完成挖掘后,试试这个:

    // 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);
    }