简体   繁体   中英

Socket.io Client error 404 not found

I am getting a 404 error (client side) on productive system. If i run the server in local network, everything is fine.

Failed to load resource: the server responded with a status of 404 (Not Found)

GET https://foo.com/socket.io/?EIO=3&transport=polling&t=MGd8e2F 404 (Not Found)

I am using socket.io Version 2.1.1 and 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');
});
...

I tried also in client.js:

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

Why can not the client connect to the server on the production system?

I found my mistake: The Load Balancer on the productive system sends the HTTP request to Port 80 but socket.io was listening to Port 443. I initiated socket.io with the wrong server: const io = require('socket.io')(httpsServer); instead of require('socket.io')(httpServer) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM