Socket.io客户端无法连接

时间:2017-12-24 18:45:30

标签: node.js ssl socket.io

我有一个在端口3000上运行的Socket.io服务器,当在本地运行它(以及网站/客户端)时,一切正常。但当我将它推送到服务器时,客户端就无法连接了。

生产服务器正在通过SSL运行,所以我假设我也需要Socket.io服务器来运行SSL。我已经像这样设置了它:

var app = express();

var fs = require('fs');

var is_production = process.env.NODE_ENV === 'production';

if(is_production){
    var options = {
        key: fs.readFileSync('/etc/letsencrypt/live/mywebsite.com/privkey.pem'),
        cert: fs.readFileSync('/etc/letsencrypt/live/mywebsite.com/cert.pem'),
        requestCert: true
    };

    var server = require('https').createServer(options, app);
}else{
    var server = require('http').createServer(app);
}

var io = require('socket.io')(server);
server.listen(3000);

这仍然无法奏效。我对Socket.io没有多少经验,所以任何帮助都会受到赞赏。另请注意,在Web服务器上设置SSL证书之前,一切正常。

客户端正在连接ws://mywebsite.com:3000。我也尝试过使用http://https://wss://,但没有任何效果。

编辑:我已尝试通过curl发出请求,但收到以下错误:

curl: (35) gnutls_handshake() failed: The TLS connection was non-properly terminated.

1 个答案:

答案 0 :(得分:0)

I couldn't figure out what the problem was, so here's what I did.

I have Nginx running on the same server to serve my website so what I ended up doing was configuring Nginx to proxy all SSL connections to port 3000 and forward them to the node.js server running on port 8080. This way Nginx takes care of the SSL so the node.js server doesn't need any additional configuration.