Blame view

node_modules/sockjs-client/lib/transport/receiver/xhr.js 1.55 KB
aaac7fed   liuqimichale   add
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
  '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;