javascript - 无法连接到 wss://(建立连接时出错:net::ERR_CONNECTION_CLOSED)

标签 javascript ssl websocket

我有带 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/

相关文章:

javascript - 产生此错误的增量 : Uncaught ReferenceError: Invalid left-hand side expression in prefix operation

ssl - 如何在 Go ReverseProxy 中使用 TLS?

.htaccess - 强制 https 时 SagePay 失败

wordpress - 用于 Wordpress 多站点的 Http 到 Https 重定向

asp.net - 从 ASP.NET 内容页面调用 onresize

javascript - 单击添加按钮我只需要八个房间

javascript - Jasmine 抛出一个错误 - 错误 : Timeout - Async callback was not invoked

angularjs - 使用 Protractor 测试 websockets 服务器

javascript - 一种阻止 WebSocket 错误显示在浏览器控制台中的方法

architecture - CQRS 和 WebSocket