gpt4 book ai didi

node.js - Socket.io 客户端未连接

转载 作者:太空宇宙 更新时间:2023-11-03 13:33:18 26 4
gpt4 key购买 nike

我有一个在端口 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.

最佳答案

我无法弄清楚问题出在哪里,所以这就是我所做的。

我在同一台服务器上运行 Nginx 来为我的网站提供服务,所以我最终做的是配置 Nginx 以代理所有到端口 3000 的 SSL 连接,并将它们转发到运行在端口 8080。这样 Nginx 会处理 SSL,因此 node.js 服务器不需要任何额外的配置。

关于node.js - Socket.io 客户端未连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47963217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com