Skip to content

Commit 805e830

Browse files
committed
Fix: reconnecting
Done as in parse-community#315 (comment) described.
1 parent 8b3fc18 commit 805e830

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

lib/src/network/parse_live_query.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class Client with WidgetsBindingObserver {
5757
} else if (_liveQueryURL.contains('http')) {
5858
_liveQueryURL = _liveQueryURL.replaceAll('http', 'ws');
5959
}
60-
Connectivity().onConnectivityChanged
60+
Connectivity()
61+
.onConnectivityChanged
6162
.listen((ConnectivityResult connectivityResult) {
63+
_currentConnectivityResult = connectivityResult;
6264
print('onConnectivityChanged:$connectivityResult');
6365
if (connectivityResult != ConnectivityResult.none) {
6466
reconnect();
@@ -89,6 +91,7 @@ class Client with WidgetsBindingObserver {
8991
bool _connecting = false;
9092
StreamController<LiveQueryClientEvent> _clientEventStreamController;
9193
Stream<LiveQueryClientEvent> _clientEventStream;
94+
ConnectivityResult _currentConnectivityResult;
9295

9396
final Map<int, Subscription> _requestSubScription = <int, Subscription>{};
9497

@@ -108,7 +111,7 @@ class Client with WidgetsBindingObserver {
108111
void didChangeAppLifecycleState(AppLifecycleState state) {
109112
super.didChangeAppLifecycleState(state);
110113
switch (state) {
111-
case AppLifecycleState.resumed:
114+
case AppLifecycleState.resumed:
112115
reconnect();
113116
break;
114117
default:
@@ -173,8 +176,8 @@ class Client with WidgetsBindingObserver {
173176
return _requestIdCount++;
174177
}
175178

176-
Future<dynamic> _connect() async {
177-
if (_connecting) {
179+
Future<void> _connect() async {
180+
if (_connecting || _currentConnectivityResult == ConnectivityResult.none) {
178181
print('already connecting');
179182
return Future<void>.value(null);
180183
}
@@ -184,6 +187,7 @@ class Client with WidgetsBindingObserver {
184187

185188
try {
186189
_webSocket = await WebSocket.connect(_liveQueryURL);
190+
_connecting = false;
187191
if (_webSocket != null && _webSocket.readyState == WebSocket.open) {
188192
if (_debug) {
189193
print('$_printConstLiveQuery: Socket opened');
@@ -220,11 +224,17 @@ class Client with WidgetsBindingObserver {
220224
ParseApiRQ.liveQuery, _debug, 'IOWebSocketChannel'));
221225
});
222226
} on Exception catch (e) {
227+
_connecting = false;
223228
_clientEventStreamController.sink.add(LiveQueryClientEvent.DISCONNECTED);
224229
if (_debug) {
225230
print('$_printConstLiveQuery: Error: ${e.toString()}');
226231
}
232+
if (!_userDisconnected &&
233+
_currentConnectivityResult != ConnectivityResult.none) {
234+
Future<dynamic>.delayed(const Duration(seconds: 2), () => reconnect());
235+
}
227236
return handleException(e, ParseApiRQ.liveQuery, _debug, 'LiveQuery');
237+
// return Future<void>.value(null);
228238
}
229239
}
230240

@@ -299,7 +309,6 @@ class Client with WidgetsBindingObserver {
299309
final Map<String, dynamic> actionData = jsonDecode(message);
300310
Subscription subscription;
301311
if (actionData.containsKey('op') && actionData['op'] == 'connected') {
302-
_connecting = false;
303312
print('ReSubScription:$_requestSubScription');
304313
_requestSubScription.values.toList().forEach((Subscription subcription) {
305314
_subscribeLiveQuery(subcription);

0 commit comments

Comments
 (0)