Skip to content

Commit 51ed91b

Browse files
committed
add oninfo event to main interface
1 parent a4f6364 commit 51ed91b

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ Where `options` is a hash which can contain:
163163
the number of expected round trips. This setting will establish a minimum,
164164
but if the calculated timeout is higher, that will be used.
165165

166+
* **transportOptions (object)**
167+
168+
Additional options to pass per transport, keyed on transport name. For example:
169+
```{ 'xhr-streaming': { headers: { Authorization: 'xxx' } } }```
170+
166171
Although the 'SockJS' object tries to emulate the 'WebSocket'
167172
behaviour, it's impossible to support all of its features. An
168173
important SockJS limitation is the fact that you're not allowed to

lib/event/info.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var inherits = require('inherits')
4+
, Event = require('./event')
5+
;
6+
7+
function InfoEvent(info, rtt, headers) {
8+
Event.call(this);
9+
this.initEvent('info', false, false);
10+
this.info = info;
11+
this.rtt = rtt;
12+
this.headers = headers;
13+
}
14+
15+
inherits(InfoEvent, Event);
16+
17+
module.exports = InfoEvent;

lib/info-ajax.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function InfoAjax(url, AjaxObject, opts) {
1818
var t0 = +new Date();
1919
this.xo = new AjaxObject('GET', url, null, opts);
2020

21-
this.xo.once('finish', function(status, text) {
21+
this.xo.once('finish', function(status, text, headers) {
2222
var info, rtt;
2323
if (status === 200) {
2424
rtt = (+new Date()) - t0;
@@ -34,7 +34,7 @@ function InfoAjax(url, AjaxObject, opts) {
3434
info = {};
3535
}
3636
}
37-
self.emit('finish', info, rtt);
37+
self.emit('finish', info, rtt, headers);
3838
self.removeAllListeners();
3939
});
4040
}

lib/info-receiver.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo, transportOptions) {
6161
self.emit('finish');
6262
}, InfoReceiver.timeout);
6363

64-
this.xo.once('finish', function(info, rtt) {
65-
debug('finish', info, rtt);
64+
this.xo.once('finish', function(info, rtt, headers) {
65+
debug('finish', info, rtt, headers);
6666
self._cleanup(true);
67-
self.emit('finish', info, rtt);
67+
self.emit('finish', info, rtt, headers || {});
6868
});
6969
};
7070

lib/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var URL = require('url-parse')
1818
, loc = require('./location')
1919
, CloseEvent = require('./event/close')
2020
, TransportMessageEvent = require('./event/trans-message')
21+
, InfoEvent = require('./event/info')
2122
, InfoReceiver = require('./info-receiver')
2223
;
2324

@@ -174,8 +175,9 @@ SockJS.OPEN = 1;
174175
SockJS.CLOSING = 2;
175176
SockJS.CLOSED = 3;
176177

177-
SockJS.prototype._receiveInfo = function(info, rtt) {
178+
SockJS.prototype._receiveInfo = function(info, rtt, headers) {
178179
debug('_receiveInfo', rtt);
180+
this.dispatchEvent(new InfoEvent(info, rtt, headers));
179181
this._ir = null;
180182
if (!info) {
181183
this._close(1002, 'Cannot connect to server');

lib/transport/driver/xhr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function XhrDriver(method, url, payload, opts) {
3939
});
4040
res.once('end', function() {
4141
debug('end');
42-
self.emit('finish', res.statusCode, responseText);
42+
self.emit('finish', res.statusCode, responseText, res.headers);
4343
self.req = null;
4444
});
4545
});

0 commit comments

Comments
 (0)