我有三台服务器
我想使用相同的IP运行所有三台服务器:端口组合,所有三台服务器都有不同的主机名。我更新了本地DNS,即/etc/hosts
,将所有三个域名解析为同一个IP,例如127.0.0.1
。
我使用VHost
根据主机名解析请求并将请求定向到适当的服务器。这是代码:
var express = require('express');
var vhost = require('vhost');
var app = express();
var https = require('https');
var mainServer = <hostname1> ;
var socketServer1 = <hostname2> ;
var socketServer2 = <hostname3>;
var server = https.createServer(<options>, app);
server.listen(<somePort>,function(){
console.log('VHost running');
});
var mainApp = require('./app.js');
var socketServerApp1 = require('./socketServer1');
var socketServerApp2 = require('./socketServer2');
app.use(vhost(mainServer, mainApp));
app.use(vhost(socketServer1, socketServerApp1(server)));
app.use(vhost(socketServer2, socketServerApp2(server)));
我在上面的代码中面临的问题是,如果我尝试运行两个套接字服务器,浏览器会抛出如下错误:
WebSocket connection to 'wss://socketServer1:8443/socket.io/?EIO=3&transport=websocket&sid=qlSXz3os5eDI8c_EAAAC' failed: Connection closed before receiving a handshake response
但如果一次只运行一个套接字服务器,那么套接字服务器的工作正常,即如果我评论app.use之一:
app.use(vhost(mainServer, mainApp));
app.use(vhost(socketServer1, socketServerApp1(server)));
// app.use(vhost(socketServer1, socketServerApp2(server)));
是否无法在同一IP上运行两个套接字服务器:端口组合?
答案 0 :(得分:0)
问题是两个套接字服务器都有单独的列表器,这导致了错误。将套接字列表器移动到vhost应用程序可以解决问题。以下是代码:
var express = require('express');
var vhost = require('vhost');
var app = express();
var https = require('https');
var socketio = require('socket.io');
var mainServer = <hostname1> ;
var socketServer1 = <hostname2> ;
var socketServer2 = <hostname3>;
var server = https.createServer(<options>, app);
var io = socketio.listen(server);
server.listen(<somePort>,function(){
console.log('VHost running');
});
var mainApp = require('./app.js');
var socketServerApp1 = require('./socketServer1');
var socketServerApp2 = require('./socketServer2');
app.use(vhost(mainServer, mainApp));
app.use(vhost(socketServer1, socketServerApp1(io)));
app.use(vhost(socketServer2, socketServerApp2(io)));