All files / src/networkWrapper/wrappers socketio.js

100% Statements 60/60
73.68% Branches 14/19
100% Functions 20/20
100% Lines 60/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187  17x 17x 17x 17x 17x 17x 17x           17x                 17x 9x   9x           9x 2x 1x 1x       1x 1x       2x     9x 1x     9x     2x 1x 1x       1x 1x   1x     2x                 17x 2x 2x               17x 3x 3x               17x 2x 2x               17x 2x 2x                     17x 1x                 17x 1x                 17x 1x                 17x 1x           17x 1x   1x 1x                         2x 2x 2x 2x 2x       2x 2x         1x  
function SocketIO(host, port, ssl) {
  this.host = host;
  this.port = port;
  this.ssl = ssl;
  this.socket = null;
  this.wasConnected = false;
  this.forceDisconnect = false;
  this.handlers = {
    connect: [],
    reconnect: [],
    connectError: [],
    disconnect: []
  };
  this.retrying = false;
 
  /**
   * Creates a new socket from the provided arguments
   *
   * @constructor
   * @param {boolean} autoReconnect
   * @param {int} reconnectionDelay
   */
  this.connect = function (autoReconnect, reconnectionDelay) {
    var self = this;
 
    this.socket = window.io((this.ssl ? 'https://' : 'http://') + this.host + ':' + this.port, {
      reconnection: autoReconnect,
      reconnectionDelay: reconnectionDelay,
      forceNew: true
    });
 
    this.socket.on('connect', function() {
      if (self.wasConnected) {
        self.handlers.reconnect.forEach(function(handler) {
          handler();
        });
      }
      else {
        self.handlers.connect.forEach(function(handler) {
          handler();
        });
      }
 
      self.wasConnected = true;
    });
 
    this.socket.on('connect_error', function(error) {
      onClientNetworkError(self, autoReconnect, reconnectionDelay, error);
    });
 
    this.socket.on('disconnect', function() {
      var error;
 
      if (self.forceDisconnect) {
        self.handlers.disconnect.forEach(function(handler) {
          handler();
        });
      }
      else {
        error = new Error('An error occurred, this may due that kuzzle was not ready yet');
        error.status = 500;
 
        onClientNetworkError(self, autoReconnect, reconnectionDelay, error);
      }
 
      self.forceDisconnect = false;
    });
  };
 
  /**
   * Fires the provided callback whence a connection is established
   *
   * @param {function} callback
   */
  this.onConnect = function (callback) {
    Eif (this.handlers.connect.indexOf(callback) === -1) {
      this.handlers.connect.push(callback);
    }
  };
 
  /**
   * Fires the provided callback whenever a connection error is received
   * @param {function} callback
   */
  this.onConnectError = function (callback) {
    Eif (this.handlers.connectError.indexOf(callback) === -1) {
      this.handlers.connectError.push(callback);
    }
  };
 
  /**
   * Fires the provided callback whenever a disconnection occurred
   * @param {function} callback
   */
  this.onDisconnect = function (callback) {
    Eif (this.handlers.disconnect.indexOf(callback) === -1) {
      this.handlers.disconnect.push(callback);
    }
  };
 
  /**
   * Fires the provided callback whenever a connection has been reestablished
   * @param {function} callback
   */
  this.onReconnect = function (callback) {
    Eif (this.handlers.reconnect.indexOf(callback) === -1) {
      this.handlers.reconnect.push(callback);
    }
  };
 
  /**
   * Registers a callback on a room. Once 1 message is received, fires the
   * callback and unregister it afterward.
   *
   * @param {string} roomId
   * @param {function} callback
   */
  this.once = function (roomId, callback) {
    this.socket.once(roomId, callback);
  };
 
  /**
   * Registers a callback on a room.
   *
   * @param {string} roomId
   * @param {function} callback
   */
  this.on = function (roomId, callback) {
    this.socket.on(roomId, callback);
  };
 
  /**
   * Unregisters a callback from a room.
   *
   * @param {string} roomId
   * @param {function} callback
   */
  this.off = function (roomId, callback) {
    this.socket.off(roomId, callback);
  };
 
 
  /**
   * Sends a payload to the connected server
   *
   * @param {Object} payload
   */
  this.send = function (payload) {
    this.socket.emit('kuzzle', payload);
  };
 
  /**
   * Closes the connection
   */
  this.close = function () {
    this.forceDisconnect = true;
 
    this.socket.close();
    this.socket = null;
  };
}
 
/**
 * Called when the connection closes with an error state
 *
 * @param {SocketIO}
 * @param {boolean} autoReconnect
 * @param {number} reconnectionDelay
 * @param {Error} error
 */
function onClientNetworkError(socketio, autoReconnect, reconnectionDelay, error) {
  Eif (autoReconnect && !socketio.retrying && !socketio.stopRetryingToConnect) {
    socketio.retrying = true;
    setTimeout(function () {
      socketio.retrying = false;
      socketio.connect(autoReconnect, reconnectionDelay);
    }, reconnectionDelay);
  }
 
  socketio.handlers.connectError.forEach(function(handler) {
    handler(error);
  });
}
 
 
module.exports = SocketIO;