Sha256: 249cefccfb6b87185c22acf5da225679fbbed3d03e3e624a2dab5ea1fecb2148

Contents?: true

Size: 1.55 KB

Versions: 34

Compression:

Stored size: 1.55 KB

Contents

'use strict';

var inherits = require('inherits')
  , EventEmitter = require('events').EventEmitter
  ;

var debug = function() {};
if (process.env.NODE_ENV !== 'production') {
  debug = require('debug')('sockjs-client:receiver:xhr');
}

function XhrReceiver(url, AjaxObject) {
  debug(url);
  EventEmitter.call(this);
  var self = this;

  this.bufferPosition = 0;

  this.xo = new AjaxObject('POST', url, null);
  this.xo.on('chunk', this._chunkHandler.bind(this));
  this.xo.once('finish', function(status, text) {
    debug('finish', status, text);
    self._chunkHandler(status, text);
    self.xo = null;
    var reason = status === 200 ? 'network' : 'permanent';
    debug('close', reason);
    self.emit('close', null, reason);
    self._cleanup();
  });
}

inherits(XhrReceiver, EventEmitter);

XhrReceiver.prototype._chunkHandler = function(status, text) {
  debug('_chunkHandler', status);
  if (status !== 200 || !text) {
    return;
  }

  for (var idx = -1; ; this.bufferPosition += idx + 1) {
    var buf = text.slice(this.bufferPosition);
    idx = buf.indexOf('\n');
    if (idx === -1) {
      break;
    }
    var msg = buf.slice(0, idx);
    if (msg) {
      debug('message', msg);
      this.emit('message', msg);
    }
  }
};

XhrReceiver.prototype._cleanup = function() {
  debug('_cleanup');
  this.removeAllListeners();
};

XhrReceiver.prototype.abort = function() {
  debug('abort');
  if (this.xo) {
    this.xo.close();
    debug('close');
    this.emit('close', null, 'user');
    this.xo = null;
  }
  this._cleanup();
};

module.exports = XhrReceiver;

Version data entries

34 entries across 33 versions & 12 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.18.0 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.18.2 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.16.1 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.15.2 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.18.4 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.18.1 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.14.0 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
tang-0.2.1 spec/tang_app/node_modules/sockjs-client/lib/transport/receiver/xhr.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/sockjs-client/lib/transport/receiver/xhr.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/sockjs-client/lib/transport/receiver/xhr.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/sockjs-client/lib/transport/receiver/xhr.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/sockjs-client/lib/transport/receiver/xhr.js
tang-0.2.0 spec/tang_app/node_modules/sockjs-client/lib/transport/receiver/xhr.js
tang-0.1.0 spec/tang_app/node_modules/sockjs-client/lib/transport/receiver/xhr.js
tang-0.0.9 spec/tang_app/node_modules/sockjs-client/lib/transport/receiver/xhr.js
enju_library-0.3.8 spec/dummy/node_modules/sockjs-client/lib/transport/receiver/xhr.js
ilog-0.4.1 node_modules/sockjs-client/lib/transport/receiver/xhr.js