File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -227,10 +227,14 @@ class HttpClientLink extends ClientLink {
227227 wsUrl = '$wsUrl $tokenHash ' ;
228228 }
229229
230- var socket = await HttpHelper .connectToWebSocket (
231- wsUrl,
232- useStandardWebSocket: useStandardWebSocket
233- );
230+ var socket = await awaitWithTimeout (
231+ HttpHelper .connectToWebSocket (wsUrl,
232+ useStandardWebSocket: useStandardWebSocket),
233+ // websocket initialization should take no more than 60 seconds
234+ 60000 ,
235+ (WebSocket socket){socket.close ();},
236+ (err){}
237+ );
234238
235239 _wsConnection = new WebSocketConnection (
236240 socket,
Original file line number Diff line number Diff line change 1+ part of dslink.utils;
2+
3+ Future <T > awaitWithTimeout <T >(Future <T > future, int timeoutMs,
4+ Function onSuccessAfterTimeout, Function onErrorAfterTimeout) {
5+ Completer <T > completer = new Completer ();
6+
7+ Timer timer = new Timer (new Duration (milliseconds: timeoutMs), () {
8+ if (! completer.isCompleted) {
9+ completer.completeError (new Exception ('Future timeout before complete' ));
10+ }
11+ });
12+ future.then ((T t) {
13+ if (completer.isCompleted) {
14+ onSuccessAfterTimeout (t);
15+ } else {
16+ timer.cancel ();
17+ completer.complete (t);
18+ }
19+ }).catchError ((err) {
20+ if (completer.isCompleted) {
21+ onErrorAfterTimeout (err);
22+ } else {
23+ timer.cancel ();
24+ completer.completeError (err);
25+ }
26+ });
27+
28+ return completer.future;
29+ }
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ part "src/utils/codec.dart";
1717part "src/utils/dslink_json.dart" ;
1818part "src/utils/list.dart" ;
1919part "src/utils/uri_component.dart" ;
20+ part "src/utils/promise_timeout.dart" ;
2021
2122typedef ExecutableFunction ();
2223typedef T Producer <T >();
You can’t perform that action at this time.
0 commit comments