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

  function invalidMessage() {
    var message = SyncMessage.error.format;
    message.content = {error: 'Unable to parse/handle message, invalid message format.'};
    client.sendMessage(message);
  }

  ws.onmessage = function(msg, flags) {
    var data;
    var message;
    var info;

    if(!flags || !flags.binary) {
      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);
        message = SyncMessage.parse(data);
      } catch(error) {
        log.error({client: client, err: error}, 'Unable to parse/handle client message. Data was `%s`', msg.data);
        return invalidMessage();
      }

      // Delegate ws messages to the sync protocol handler at this point
      client.handler.handleMessage(message);
    } else {
      log.warn({client: client}, 'Expected string but got binary data over web socket.');
      invalidMessage();
    }
  };

  // Send an AUTHZ response to let client know normal sync'ing can begin.
  client.state = States.LISTENING;
  client.sendMessage(SyncMessage.response.authz);
  log.debug({client: client}, 'Starting authorized client session');
}
function runClient(client) {
  var ws = client.ws;

  function invalidMessage() {
    var message = SyncMessage.error.format;
    message.content = {error: 'Unable to parse/handle message, invalid message format.'};
    client.sendMessage(message);
  }

  ws.onmessage = function(msg, flags) {
    var data;
    var message;
    var info;

    if(!flags || !flags.binary) {
      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);
        message = SyncMessage.parse(data);
      } catch(error) {
        log.error({client: client, err: error}, 'Unable to parse/handle client message. Data was `%s`', msg.data);
        return invalidMessage();
      }

      // Delegate ws messages to the sync protocol handler at this point
      client.handler.handleMessage(message);
    } else {
      log.warn({client: client}, 'Expected string but got binary data over web socket.');
      invalidMessage();
    }
  };

  // Send an AUTHZ response to let client know normal sync'ing can begin.
  client.state = States.LISTENING;
  client.sendMessage(SyncMessage.response.authz);
  log.debug({client: client}, 'Starting authorized client session');
}
function runClient ( client ) { var ws = client . ws ; function invalidMessage ( ) { var message = SyncMessage . error . format ; message . content = { error : 'Unable to parse/handle message, invalid message format.' } ; client . sendMessage ( message ) ; } ws . onmessage = function ( msg , flags ) { var data ; var message ; var info ; if ( ! flags || ! flags . binary ) { 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 ) ; message = SyncMessage . parse ( data ) ; } catch ( error ) { log . error ( { client : client , err : error } , 'Unable to parse/handle client message. Data was `%s`' , msg . data ) ; return invalidMessage ( ) ; } // Delegate ws messages to the sync protocol handler at this point client . handler . handleMessage ( message ) ; } else { log . warn ( { client : client } , 'Expected string but got binary data over web socket.' ) ; invalidMessage ( ) ; } } ; // Send an AUTHZ response to let client know normal sync'ing can begin. client . state = States . LISTENING ; client . sendMessage ( SyncMessage . response . authz ) ; log . debug ( { client : client } , 'Starting authorized client session' ) ; }
Run the client normally through protocol steps.
Run the client normally through protocol steps .