1
|
'use strict';
|
2
|
|
3
|
var EventEmitter = require('events').EventEmitter
|
4
|
, inherits = require('inherits')
|
5
|
, JSON3 = require('json3')
|
6
|
, objectUtils = require('./utils/object')
|
7
|
;
|
8
|
|
9
|
var debug = function() {};
|
10
|
if (process.env.NODE_ENV !== 'production') {
|
11
|
debug = require('debug')('sockjs-client:info-ajax');
|
12
|
}
|
13
|
|
14
|
function InfoAjax(url, AjaxObject) {
|
15
|
EventEmitter.call(this);
|
16
|
|
17
|
var self = this;
|
18
|
var t0 = +new Date();
|
19
|
this.xo = new AjaxObject('GET', url);
|
20
|
|
21
|
this.xo.once('finish', function(status, text) {
|
22
|
var info, rtt;
|
23
|
if (status === 200) {
|
24
|
rtt = (+new Date()) - t0;
|
25
|
if (text) {
|
26
|
try {
|
27
|
info = JSON3.parse(text);
|
28
|
} catch (e) {
|
29
|
debug('bad json', text);
|
30
|
}
|
31
|
}
|
32
|
|
33
|
if (!objectUtils.isObject(info)) {
|
34
|
info = {};
|
35
|
}
|
36
|
}
|
37
|
self.emit('finish', info, rtt);
|
38
|
self.removeAllListeners();
|
39
|
});
|
40
|
}
|
41
|
|
42
|
inherits(InfoAjax, EventEmitter);
|
43
|
|
44
|
InfoAjax.prototype.close = function() {
|
45
|
this.removeAllListeners();
|
46
|
this.xo.close();
|
47
|
};
|
48
|
|
49
|
module.exports = InfoAjax;
|