server/lib/client-manager.js
function initClient(client) {
  var ws = client.ws;

  client.state = States.CONNECTING;

  // Wait until we get the user's token so we can finish authorizing
  ws.onmessage = function(msg) {
    var data;
    var info;

    try {
      // Keep track of how much data we receive
      info = client.info();
      if(info) {
        info.bytesReceived += Buffer.byteLength(msg.data, 'utf8');
      }

      data = JSON.parse(msg.data);
    } catch(err) {
      log.error({client: client, err: err}, 'Error parsing client token. Data was `%s`', msg.data);
      ClientInfo.remove(token);
      client.close({
        code: 1011,
        message: 'Error: token could not be parsed.'
      });
      return;
    }

    // Authorize user
    var token = data.token;
    var username = WebsocketAuth.getAuthorizedUsername(token);
    if (!username) {
      log.warn({client: client}, 'Client sent an invalid or expired token (could not get username): token=%s', token);
      ClientInfo.remove(token);
      client.close({
        code: 1008,
        message: 'Error: invalid token.'
      });
      return;
    }

    // Update client details now that he/she is authenticated
    client.id = token;
    client.username = username;
    client.fs = filesystem.create(username);
    ClientInfo.update(client);

    log.info({client: client}, 'Client connected');

    runClient(client);
  };
}
function initClient(client) {
  var ws = client.ws;

  client.state = States.CONNECTING;

  // Wait until we get the user's token so we can finish authorizing
  ws.onmessage = function(msg) {
    var data;
    var info;

    try {
      // Keep track of how much data we receive
      info = client.info();
      if(info) {
        info.bytesReceived += Buffer.byteLength(msg.data, 'utf8');
      }

      data = JSON.parse(msg.data);
    } catch(err) {
      log.error({client: client, err: err}, 'Error parsing client token. Data was `%s`', msg.data);
      ClientInfo.remove(token);
      client.close({
        code: 1011,
        message: 'Error: token could not be parsed.'
      });
      return;
    }

    // Authorize user
    var token = data.token;
    var username = WebsocketAuth.getAuthorizedUsername(token);
    if (!username) {
      log.warn({client: client}, 'Client sent an invalid or expired token (could not get username): token=%s', token);
      ClientInfo.remove(token);
      client.close({
        code: 1008,
        message: 'Error: invalid token.'
      });
      return;
    }

    // Update client details now that he/she is authenticated
    client.id = token;
    client.username = username;
    client.fs = filesystem.create(username);
    ClientInfo.update(client);

    log.info({client: client}, 'Client connected');

    runClient(client);
  };
}
function initClient ( client ) { var ws = client . ws ; client . state = States . CONNECTING ; // Wait until we get the user's token so we can finish authorizing ws . onmessage = function ( msg ) { var data ; var info ; try { // Keep track of how much data we receive info = client . info ( ) ; if ( info ) { info . bytesReceived += Buffer . byteLength ( msg . data , 'utf8' ) ; } data = JSON . parse ( msg . data ) ; } catch ( err ) { log . error ( { client : client , err : err } , 'Error parsing client token. Data was `%s`' , msg . data ) ; ClientInfo . remove ( token ) ; client . close ( { code : 1011 , message : 'Error: token could not be parsed.' } ) ; return ; } // Authorize user var token = data . token ; var username = WebsocketAuth . getAuthorizedUsername ( token ) ; if ( ! username ) { log . warn ( { client : client } , 'Client sent an invalid or expired token (could not get username): token=%s' , token ) ; ClientInfo . remove ( token ) ; client . close ( { code : 1008 , message : 'Error: invalid token.' } ) ; return ; } // Update client details now that he/she is authenticated client . id = token ; client . username = username ; client . fs = filesystem . create ( username ) ; ClientInfo . update ( client ) ; log . info ( { client : client } , 'Client connected' ) ; runClient ( client ) ; } ; }
Handle initial connection and authentication, bind user data
to client, including filesystem, and switch the client to normal
run mode.
Handle initial connection and authentication bind user data to client including filesystem and switch the client to normal run mode .