找不到Socket.io客户端错误404

时间:2018-06-22 11:18:34

标签: node.js sockets socket.io

我在生产系统上遇到404错误(客户端)。如果我在局域网中运行服务器,一切都很好。

  

无法加载资源:服务器的响应状态为404(未找到)

     

获取https://foo.com/socket.io/?EIO=3&transport=polling&t=MGd8e2F 404(未找到)

我正在使用socket.io版本2.1.1和Express 4。

Server.js

...
var http = require('http');
var https = require('https');
var app = express();
httpServer = http.createServer(app);
httpsServer = https.createServer(options, app);
...
const io = require('socket.io')(httpsServer);

httpServer.listen(process.env.SERVER_HTTP_PORT);
httpsServer.listen(process.env.SERVER_HTTPS_PORT);

io.use(function (socket, next) {
    //Middleware (express-session)
    e_session(socket.request, socket.request.res, next);
});
io.on('connection', function (client) {
   ...
}

client.js

const socket = io('https://' + window.location.hostname + ':443');

socket.on('connect', function (data) {
  socket.emit('join', 'Server Connected to Client');
});
...

我也在client.js中尝试过:

const socket = io();
const socket = io('https://' + window.location.hostname);

为什么客户端不能连接到生产系统上的服务器?

1 个答案:

答案 0 :(得分:0)

我发现了我的错误:生产系统上的负载均衡器将HTTP请求发送到端口80,但是socket.io正在侦听端口443。我用错误的服务器启动了socket.io: const io = require('socket.io')(httpsServer);代替 require('socket.io')(httpServer)