2021年5月7日星期五

Why does socket.io give a ERR_CONNECTION_TIMED_OUT error?

The browser console shows:

socket.io.min.js:6 GET http://www.europasprak.com:9001/socket.io/?EIO=4&transport=polling&t=NbAYx93 net::ERR_CONNECTION_TIMED_OUT  

The error can be seen live at http://www.europasprak.com/elearning/subscription/2938/course by first logging at http://www.europasprak.com/engine/modules/user/login.php with using the user demo@demo.com with the demo password.

The client connection:

var elearningSocket;  $(function() {    if ('undefined' != typeof io && 'undefined' == typeof elearningSocket) {      console.log("Creating a socket on $gSocketHostname:$NODEJS_SOCKET_PORT/elearning");      elearningSocket = io.connect('$gSocketHostname:$NODEJS_SOCKET_PORT/elearning');    }  });  

My socket.io.min.js version is:

/*!   * Socket.IO v4.0.1   * (c) 2014-2021 Guillermo Rauch   * Released under the MIT License.   */  

The server implementation:

var http = require('http');  var https = require('https');  var cors = require('cors');  var connect = require('connect');  var cookie = require('cookie');  var path = require('path');  var fs = require('fs');  var redis = require('redis');  var ioredis = require('socket.io-redis');  var socketio = require('socket.io');    var utils = require('./utils.js');  var config = require('./config');    var sslKey = '';  var sslCertificate = '';  var sslChain = '';  if (fs.existsSync(config.ssl.path + config.ssl.key)) {    sslKey = fs.readFileSync(path.resolve(config.ssl.path + config.ssl.key));    sslCertificate = fs.readFileSync(path.resolve(config.ssl.path + config.ssl.certificate));    sslChain = fs.readFileSync(path.resolve(config.ssl.path + config.ssl.chain));    console.log("The virtual host HAS an SSL private key");  } else {    console.log("The virtual host DOESN'T have an SSL private key");  }    console.log("Configuring the server for HTTP");  console.log("The HTTP server is used by the healthcheck even if the socket is served on the HTTPS server");  var httpServer = http.createServer(utils.httpHandler);  httpServer.listen(config.socketio.port, function() {    console.log('The NodeJS HTTP server [port: ' + config.socketio.port + '] is listening...');  });    if (sslKey) {    console.log("Configuring the server for HTTPS");    var options = {      key: sslKey,      cert: sslCertificate,      ca: sslChain,      requestCert: false,      rejectUnauthorized: false    };    var httpsServer = https.createServer(options, utils.httpHandler);    httpsServer.listen(config.socketio.sslport, function() {      console.log('The NodeJS HTTPS server [port: ' + config.socketio.sslport + '] is listening...');    });  }    module.exports.io = socketio(httpsServer, {    cors: {      origin: '*',      methods: [        'GET',        'POST'      ],      allowedHeaders: [],      credentials: true    }  });  console.log(module.exports.io);    module.exports.io.adapter(ioredis({ host: config.redis.hostname, port: config.redis.port }));  var redisClient = redis.createClient(config.redis.port, config.redis.hostname);    module.exports.io.use(function (socket, handler) {    if (socket.request.headers.cookie) {      socket.request.cookies = cookie.parse(decodeURIComponent(socket.request.headers.cookie));      socket.request.sessionID = socket.request.cookies['PHPSESSID'];      socket.request.socketSessionId = socket.request.cookies['socketSessionId'];      console.log("Authorization attempt with sessionID: " + socket.request.sessionID + " and socketSessionId: " + socket.request.socketSessionId);      redisClient.get("PHPREDIS_SESSION:" + socket.request.sessionID, function (error, reply) {        if (error) {          console.log("The redis client had an error: " + error);          return handler(new Error('The connection was refused because the redis client had an error.'));        } else if (!reply) {          console.log('The connection was refused because the redis client did not find the sessionID.');          return handler(new Error('The connection was refused because the redis client did not find the sessionID.'));        } else {          var redisSocketSessionId = utils.getRedisValue(reply, "socketSessionId");          if ('undefined' == typeof socket.request.socketSessionId || redisSocketSessionId != socket.request.socketSessionId) {            console.log('The connection was refused because the socketSessionId was invalid.');            return handler(new Error('The connection was refused because the socketSessionId was invalid.'));          } else {            console.log('The connection was granted.');            handler();          }        }      });    } else {      console.log('The connection was refused because no cookie was transmitted.');      return handler(new Error('The connection was refused because no cookie was transmitted.'));    }  });  
https://stackoverflow.com/questions/67444571/why-does-socket-io-give-a-err-connection-timed-out-error May 08, 2021 at 02:04PM

没有评论:

发表评论