app/ws/ClientStore.js
// adds a ws to the local store, and initialises a heartbeat pinger
adds a ws to the local store, and initialises a heartbeat pinger
adds a ws to the local store and initialises a heartbeat pinger
add( ws ) {
    this.redisClient.get( ws.clientId, ( err, reply ) => {
      if ( reply !== null ) {
        let attemptedId = ws.clientId
        ws.clientId = mongoose.Types.ObjectId( ).toString( )
        ws.send( JSON.stringify( { message: 'Warning: this client is open in another file.', newId: ws.clientId } ) )
        winston.debug( `duplicate client id found (${attemptedId}). Sent him a new one: ${ws.clientId}.` )
      }
      this.redisClient.set( ws.clientId, ws.clientId )
      ws.alive = true
      ws.missedPingsCount = 0
      ws.pinger = setInterval( ws => {
        if ( !ws.alive ) {
          ws.missedPingsCount++
          if ( ws.missedPingsCount > 5 ) {
            ws.send( 'Warning: you missed 5 mins of pings. After 10, you will be kicked!' )
          }
          if ( ws.missedPingsCount > 10 ) {
            winston.error( chalk.red( 'Removing client socket after 10 mins of inactivity.' ) )
            ws.send( 'You missed 10 minutes of pings. Bye!' )
            this.remove( ws )
            return
          }
          ws.alive = false
        }
        ws.alive = false
        ws.send( 'ping' )
      }, 30000, ws ) 
      this.clients.push( ws )
      winston.debug( chalk.green( "(add) Clients: " + this.clients.length + "." ) )
      winston.debug( chalk.blue( `There are now ${this.clients.length} ws clients in ${process.pid}.` ) )
    } )
  }
add ( ws ) { this . redisClient . get ( ws . clientId , ( err , reply ) => { if ( reply !== null ) { let attemptedId = ws . clientId ws . clientId = mongoose . Types . ObjectId ( ) . toString ( ) ws . send ( JSON . stringify ( { message : 'Warning: this client is open in another file.' , newId : ws . clientId } ) ) winston . debug ( ` ${ attemptedId } ${ ws . clientId } ` ) } this . redisClient . set ( ws . clientId , ws . clientId ) ws . alive = true ws . missedPingsCount = 0 ws . pinger = setInterval ( ws => { if ( ! ws . alive ) { ws . missedPingsCount ++ if ( ws . missedPingsCount > 5 ) { ws . send ( 'Warning: you missed 5 mins of pings. After 10, you will be kicked!' ) } if ( ws . missedPingsCount > 10 ) { winston . error ( chalk . red ( 'Removing client socket after 10 mins of inactivity.' ) ) ws . send ( 'You missed 10 minutes of pings. Bye!' ) this . remove ( ws ) return } ws . alive = false } ws . alive = false ws . send ( 'ping' ) } , 30000 , ws ) this . clients . push ( ws ) winston . debug ( chalk . green ( "(add) Clients: " + this . clients . length + "." ) ) winston . debug ( chalk . blue ( ` ${ this . clients . length } ${ process . pid } ` ) ) } ) }
adds a ws to the local store, and initialises a heartbeat pinger
adds a ws to the local store and initialises a heartbeat pinger
// add his identity to the redis datastore // setup vars // setup pinging system // HIC SVNT DRACONES: when ping was every 60s, socket connections would get dropped // on the deploted server. Perhaps due to a TTL in nginx on hestia. // ping every 30s. // push to my amazing datastore