arnaucube
arnaucube/raspberryGPIOhtmlserverPublic Self-hosted repository
raspberryGPIOhtmlserver/node_modules/engine.io-client/lib/transport.js
node_modules/engine.io-client/lib/transport.jsRaw
  1/**
  2 * Module dependencies.
  3 */
  4
  5var parser = require('engine.io-parser');
  6var Emitter = require('component-emitter');
  7
  8/**
  9 * Module exports.
 10 */
 11
 12module.exports = Transport;
 13
 14/**
 15 * Transport abstract constructor.
 16 *
 17 * @param {Object} options.
 18 * @api private
 19 */
 20
 21function Transport (opts) {
 22  this.path = opts.path;
 23  this.hostname = opts.hostname;
 24  this.port = opts.port;
 25  this.secure = opts.secure;
 26  this.query = opts.query;
 27  this.timestampParam = opts.timestampParam;
 28  this.timestampRequests = opts.timestampRequests;
 29  this.readyState = '';
 30  this.agent = opts.agent || false;
 31  this.socket = opts.socket;
 32  this.enablesXDR = opts.enablesXDR;
 33}
 34
 35/**
 36 * Mix in `Emitter`.
 37 */
 38
 39Emitter(Transport.prototype);
 40
 41/**
 42 * A counter used to prevent collisions in the timestamps used
 43 * for cache busting.
 44 */
 45
 46Transport.timestamps = 0;
 47
 48/**
 49 * Emits an error.
 50 *
 51 * @param {String} str
 52 * @return {Transport} for chaining
 53 * @api public
 54 */
 55
 56Transport.prototype.onError = function (msg, desc) {
 57  var err = new Error(msg);
 58  err.type = 'TransportError';
 59  err.description = desc;
 60  this.emit('error', err);
 61  return this;
 62};
 63
 64/**
 65 * Opens the transport.
 66 *
 67 * @api public
 68 */
 69
 70Transport.prototype.open = function () {
 71  if ('closed' == this.readyState || '' == this.readyState) {
 72    this.readyState = 'opening';
 73    this.doOpen();
 74  }
 75
 76  return this;
 77};
 78
 79/**
 80 * Closes the transport.
 81 *
 82 * @api private
 83 */
 84
 85Transport.prototype.close = function () {
 86  if ('opening' == this.readyState || 'open' == this.readyState) {
 87    this.doClose();
 88    this.onClose();
 89  }
 90
 91  return this;
 92};
 93
 94/**
 95 * Sends multiple packets.
 96 *
 97 * @param {Array} packets
 98 * @api private
 99 */
100
101Transport.prototype.send = function(packets){
102  if ('open' == this.readyState) {
103    this.write(packets);
104  } else {
105    throw new Error('Transport not open');
106  }
107};
108
109/**
110 * Called upon open
111 *
112 * @api private
113 */
114
115Transport.prototype.onOpen = function () {
116  this.readyState = 'open';
117  this.writable = true;
118  this.emit('open');
119};
120
121/**
122 * Called with data.
123 *
124 * @param {String} data
125 * @api private
126 */
127
128Transport.prototype.onData = function(data){
129  var packet = parser.decodePacket(data, this.socket.binaryType);
130  this.onPacket(packet);
131};
132
133/**
134 * Called with a decoded packet.
135 */
136
137Transport.prototype.onPacket = function (packet) {
138  this.emit('packet', packet);
139};
140
141/**
142 * Called upon close.
143 *
144 * @api private
145 */
146
147Transport.prototype.onClose = function () {
148  this.readyState = 'closed';
149  this.emit('close');
150};