source: trip-planner-front/node_modules/sockjs-client/lib/info-receiver.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
1'use strict';
2
3var EventEmitter = require('events').EventEmitter
4 , inherits = require('inherits')
5 , urlUtils = require('./utils/url')
6 , XDR = require('./transport/sender/xdr')
7 , XHRCors = require('./transport/sender/xhr-cors')
8 , XHRLocal = require('./transport/sender/xhr-local')
9 , XHRFake = require('./transport/sender/xhr-fake')
10 , InfoIframe = require('./info-iframe')
11 , InfoAjax = require('./info-ajax')
12 ;
13
14var debug = function() {};
15if (process.env.NODE_ENV !== 'production') {
16 debug = require('debug')('sockjs-client:info-receiver');
17}
18
19function InfoReceiver(baseUrl, urlInfo) {
20 debug(baseUrl);
21 var self = this;
22 EventEmitter.call(this);
23
24 setTimeout(function() {
25 self.doXhr(baseUrl, urlInfo);
26 }, 0);
27}
28
29inherits(InfoReceiver, EventEmitter);
30
31// TODO this is currently ignoring the list of available transports and the whitelist
32
33InfoReceiver._getReceiver = function(baseUrl, url, urlInfo) {
34 // determine method of CORS support (if needed)
35 if (urlInfo.sameOrigin) {
36 return new InfoAjax(url, XHRLocal);
37 }
38 if (XHRCors.enabled) {
39 return new InfoAjax(url, XHRCors);
40 }
41 if (XDR.enabled && urlInfo.sameScheme) {
42 return new InfoAjax(url, XDR);
43 }
44 if (InfoIframe.enabled()) {
45 return new InfoIframe(baseUrl, url);
46 }
47 return new InfoAjax(url, XHRFake);
48};
49
50InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) {
51 var self = this
52 , url = urlUtils.addPath(baseUrl, '/info')
53 ;
54 debug('doXhr', url);
55
56 this.xo = InfoReceiver._getReceiver(baseUrl, url, urlInfo);
57
58 this.timeoutRef = setTimeout(function() {
59 debug('timeout');
60 self._cleanup(false);
61 self.emit('finish');
62 }, InfoReceiver.timeout);
63
64 this.xo.once('finish', function(info, rtt) {
65 debug('finish', info, rtt);
66 self._cleanup(true);
67 self.emit('finish', info, rtt);
68 });
69};
70
71InfoReceiver.prototype._cleanup = function(wasClean) {
72 debug('_cleanup');
73 clearTimeout(this.timeoutRef);
74 this.timeoutRef = null;
75 if (!wasClean && this.xo) {
76 this.xo.close();
77 }
78 this.xo = null;
79};
80
81InfoReceiver.prototype.close = function() {
82 debug('close');
83 this.removeAllListeners();
84 this._cleanup(false);
85};
86
87InfoReceiver.timeout = 8000;
88
89module.exports = InfoReceiver;
Note: See TracBrowser for help on using the repository browser.