SocketCluster / socketcluster

Highly scalable realtime pub/sub and RPC framework
https://socketcluster.io
MIT License
6.15k stars 314 forks source link

Error while getting Socket.Clients #460

Closed ansubur closed 5 years ago

ansubur commented 5 years ago

Just trying get a particular socket with id.

   var serverSocket = scServer.client[socket.id];

The above causes: [Error] TypeError: Cannot read property 'AheT4t9bLxMGoYG9AAAA' of undefined.

Full code:

class Worker extends SCWorker {
  run() {
    console.log('   >> Worker PID:', process.pid);
    var environment = this.options.environment;

    var app = express();

    var httpServer = this.httpServer;
    var scServer = this.scServer;

    if (environment === 'dev') {
      // Log every HTTP request. See https://github.com/expressjs/morgan for other
      // available formats.
      app.use(morgan('dev'));
    }
    app.use(serveStatic(path.resolve(__dirname, 'public')));

    // Add GET /health-check express route
    healthChecker.attach(this, app);

    httpServer.on('request', app);

      socket.on('subscribe', function(channelname){
        console.log('subscription completed for ' + channelname + ', ' + socket.id);
        console.log('All Channels: ' + socket.subscriptions());

        console.log('client count : ' + scServer.clientsCount);
        var serverSocket = scServer.client[socket.id];

        if( serverSocket != null)
        {
          console.log('Server Socket Found');
        }
        else{
          console.log('server socket not found!');
        }

      })

      socket.on('disconnect', function () {
        clearInterval(interval);
      }); 
    });
  }
}

new Worker();
jondubois commented 5 years ago

It should be scServer.clients[socket.id] plural.

ansubur commented 5 years ago

Was never able to find it. Thanks a lot! I'm used to .net IDE & hence intellisense minded ;)