Skip to content

Commit 3427e74

Browse files
bripkensdarrachequesne
authored andcommitted
Enable definition of timeouts for xhr-polling
The definition of timeouts is desirable to cancel long outstanding requests automatically via browser mechanisms. Specifically, it can happen that lots of pending XHR requests are scheduled, but are never purged by the browser due to the lack of a timeout definition.
1 parent cea0dff commit 3427e74

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Exposed as `eio` in the browser standalone build.
159159
- `String` | `ArrayBuffer`: utf-8 encoded data or ArrayBuffer containing
160160
binary data
161161
- `close`
162-
- Fired upon disconnection. In compliance with the WebSocket API spec, this event may be
162+
- Fired upon disconnection. In compliance with the WebSocket API spec, this event may be
163163
fired even if the `open` event does not occur (i.e. due to connection error or `close()`).
164164
- `error`
165165
- Fired when an error occurs.
@@ -224,6 +224,7 @@ Exposed as `eio` in the browser standalone build.
224224
- `threshold` (`Number`): data is compressed only if the byte size is above this value. This option is ignored on the browser. (`1024`)
225225
- `extraHeaders` (`Object`): Headers that will be passed for each request to the server (via xhr-polling and via websockets). These values then can be used during handshake or for special proxies. Can only be used in Node.js client environment.
226226
- `onlyBinaryUpgrades` (`Boolean`): whether transport upgrades should be restricted to transports supporting binary data (`false`)
227+
- `requestTimeout` (`Number`): Timeout for xhr-polling requests in milliseconds (`0`)
227228
- `send`
228229
- Sends a message to the server
229230
- **Parameters**
@@ -290,4 +291,3 @@ See the `Tests` section above for how to run tests before submitting any patches
290291
## License
291292
292293
MIT - Copyright (c) 2014 Automattic, Inc.
293-

lib/transports/polling-xhr.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function empty () {}
3030

3131
function XHR (opts) {
3232
Polling.call(this, opts);
33+
this.requestTimeout = opts.requestTimeout;
3334

3435
if (global.location) {
3536
var isSSL = 'https:' === location.protocol;
@@ -84,6 +85,7 @@ XHR.prototype.request = function (opts) {
8485
opts.ca = this.ca;
8586
opts.ciphers = this.ciphers;
8687
opts.rejectUnauthorized = this.rejectUnauthorized;
88+
opts.requestTimeout = this.requestTimeout;
8789

8890
// other options for Node.js client
8991
opts.extraHeaders = this.extraHeaders;
@@ -147,6 +149,7 @@ function Request (opts) {
147149
this.isBinary = opts.isBinary;
148150
this.supportsBinary = opts.supportsBinary;
149151
this.enablesXDR = opts.enablesXDR;
152+
this.requestTimeout = opts.requestTimeout;
150153

151154
// SSL options for Node.js client
152155
this.pfx = opts.pfx;
@@ -228,6 +231,10 @@ Request.prototype.create = function () {
228231
xhr.withCredentials = true;
229232
}
230233

234+
if (this.requestTimeout) {
235+
xhr.timeout = this.requestTimeout;
236+
}
237+
231238
if (this.hasXDR()) {
232239
xhr.onload = function () {
233240
self.onLoad();

0 commit comments

Comments
 (0)