Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout option #45

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pubspec.lock
.dart_tool
.packages
node_modules
/.idea
14 changes: 9 additions & 5 deletions lib/sockjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,13 @@
debug = require('debug')('sockjs-client:info-receiver');
}

function InfoReceiver(baseUrl, urlInfo) {
function InfoReceiver(baseUrl, urlInfo, timeout) {
debug(baseUrl);
var self = this;
EventEmitter.call(this);

setTimeout(function() {
self.doXhr(baseUrl, urlInfo);
self.doXhr(baseUrl, urlInfo, timeout);
}, 0);
}

Expand All @@ -547,19 +547,23 @@
return new InfoAjax(url, XHRFake);
};

InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) {
InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo, timeout) {
var self = this
, url = urlUtils.addPath(baseUrl, '/info')
;
debug('doXhr', url);

this.xo = InfoReceiver._getReceiver(baseUrl, url, urlInfo);


if (timeout === undefined) {
timeout = InfoReceiver.timeout
Comment on lines +559 to +560
Copy link

@patkujawa-wf patkujawa-wf Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If sockJS's internally calculated
/// timeout is higher, that will be used instead.

😕 if true, shouldn't this conditional be < ? But maybe also set a lower limit?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's min timeout!

Copy link
Member Author

@tomconnell-wf tomconnell-wf Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, whoops, I must have had that slip through the cracks somewhere. I'll see what the sockjs maintainer says. (I don't know if they will want this or that)

}
this.timeoutRef = setTimeout(function() {
debug('timeout');
self._cleanup(false);
self.emit('finish');
}, InfoReceiver.timeout);
}, timeout);

this.xo.once('finish', function(info, rtt) {
debug('finish', info, rtt);
Expand Down Expand Up @@ -731,7 +735,7 @@
, sameScheme: urlUtils.isSchemeEqual(this.url, loc.href)
};

this._ir = new InfoReceiver(this.url, this._urlInfo);
this._ir = new InfoReceiver(this.url, this._urlInfo, options.timeout);
this._ir.once('finish', this._receiveInfo.bind(this));
}

Expand Down
8 changes: 6 additions & 2 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,14 @@ class SockJSOptions {
/// can be useful if you need to disable certain fallback transports.
final List<String> transports;

/// The minimum timeout, in milliseconds. If sockJS's internally calculated
/// timeout is higher, that will be used instead.
final int timeout;
patkujawa-wf marked this conversation as resolved.
Show resolved Hide resolved

/// Construct a [SockJSOptions] instance to be passed to the [SockJSClient]
/// constructor.
SockJSOptions({this.server, this.transports});
SockJSOptions({this.server, this.transports, this.timeout});

js_interop.SockJSOptions _toJs() =>
js_interop.SockJSOptions(server: server, transports: transports);
js_interop.SockJSOptions(server: server, transports: transports, timeout: timeout);
}
2 changes: 1 addition & 1 deletion lib/src/js_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SockJSOptions {
/// Example:
///
/// {server: 'foo', transports: ['websocket', 'xhr-polling']}
external factory SockJSOptions({String server, List<String> transports});
external factory SockJSOptions({String server, List<String> transports, int timeout});

/// String to append to url for actual data connection.
///
Expand Down