我的 socket io 和 Node express js 服务器现在都在监听相同的端口。它托管在 AWS ElasticBeanStalk 中。它运作良好。 现在由于安全限制,必须将 socket io 移动到不同的端口。它是与 node js 应用程序相同的代码库的一部分。 我可以为 socket io 监听不同的端口吗?这行得通吗?我必须在 ElasticBeanStalk 中做哪些更改?有没有新的端口需要打开或者端口有什么限制吗?
最佳答案
docs给出通过标准 Node HTTP 通过 Express 或 连接的示例。所以从技术上讲,您可以将两者融合在一起并在不同的端口上收听。
var exApp = require('express')();
var exServer = require('http').Server(exApp);
var ioApp = require('http').createServer(handler);
var io = require('socket.io')(ioApp);
exServer.listen(80);
ioApp.listen(81);
function handler (req, res) {
res.writeHead(200).end({});
}
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// Then whatever you want to do with `io`, and `exApp`.
如果您转到 localhost:80
,您将获得 Express 应用程序服务,如果您向 localhost:81
发送 socket.io 请求,它应该触发io.connection()
我还没有对此进行技术测试,所以如果我离题了请告诉我,我会进一步考虑。
关于amazon-web-services - 如何让express js和socket io监听不同的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47627244/