@@ -32,13 +32,9 @@ class ConnectionCubit extends Cubit<ConnectionState> {
32
32
33
33
_connectionInformation = connectionInformation;
34
34
35
- if (_api is BinaryAPI ) {
36
- _setupConnectivityListener ();
37
- }
35
+ _connect (_connectionInformation);
38
36
39
37
_startKeepAliveTimer ();
40
-
41
- _connect (_connectionInformation);
42
38
}
43
39
44
40
final String _key = '${UniqueKey ()}' ;
@@ -74,6 +70,9 @@ class ConnectionCubit extends Cubit<ConnectionState> {
74
70
/// Gets app id of websocket.
75
71
static String get appId => _connectionInformation.appId;
76
72
73
+ /// Streamsubscription for connectivity.
74
+ StreamSubscription <ConnectivityResult >? connectivitySubscription;
75
+
77
76
/// Reconnect to Websocket.
78
77
Future <void > reconnect ({ConnectionInformation ? connectionInformation}) async {
79
78
emit (const ConnectionDisconnectedState ());
@@ -111,11 +110,9 @@ class ConnectionCubit extends Cubit<ConnectionState> {
111
110
emit (const ConnectionConnectedState ());
112
111
}
113
112
},
114
- onDone: (String key) async {
113
+ onDone: (String key) {
115
114
if (_key == key) {
116
- await _api! .disconnect ();
117
-
118
- emit (const ConnectionDisconnectedState ());
115
+ unawaited (reconnect ());
119
116
}
120
117
},
121
118
onError: (String key) {
@@ -124,28 +121,32 @@ class ConnectionCubit extends Cubit<ConnectionState> {
124
121
}
125
122
},
126
123
);
124
+
125
+ if (_api is BinaryAPI ) {
126
+ _setupConnectivityListener ();
127
+ }
127
128
}
128
129
129
- void _setupConnectivityListener () =>
130
- Connectivity ().onConnectivityChanged.listen (
131
- (ConnectivityResult status) async {
132
- final bool isConnectedToNetwork =
133
- status == ConnectivityResult .mobile ||
134
- status == ConnectivityResult .wifi;
135
-
136
- if (isConnectedToNetwork) {
137
- final bool isConnected = await _ping ();
138
-
139
- if (! isConnected) {
140
- await reconnect ();
141
- }
142
- } else if (status == ConnectivityResult .none) {
143
- emit (const ConnectionDisconnectedState ());
130
+ void _setupConnectivityListener () {
131
+ connectivitySubscription ?? = Connectivity ().onConnectivityChanged.listen (
132
+ (ConnectivityResult status) async {
133
+ final bool isConnectedToNetwork = status == ConnectivityResult .mobile ||
134
+ status == ConnectivityResult .wifi;
135
+
136
+ if (isConnectedToNetwork) {
137
+ final bool isConnected = await _ping ();
138
+
139
+ if (! isConnected) {
140
+ await reconnect ();
144
141
}
145
- },
146
- );
142
+ } else if (status == ConnectivityResult .none) {
143
+ emit (const ConnectionDisconnectedState ());
144
+ }
145
+ },
146
+ );
147
+ }
147
148
148
- void _startKeepAliveTimer () {
149
+ Future < void > _startKeepAliveTimer () async {
149
150
if (_connectivityTimer == null || ! _connectivityTimer! .isActive) {
150
151
_connectivityTimer =
151
152
Timer .periodic (_connectivityCheckInterval, (Timer timer) => _ping ());
@@ -156,7 +157,6 @@ class ConnectionCubit extends Cubit<ConnectionState> {
156
157
try {
157
158
final PingResponse response =
158
159
await PingResponse .pingMethod ().timeout (_pingTimeout);
159
-
160
160
return response.ping == PingEnum .pong;
161
161
} on Exception catch (_) {
162
162
return false ;
@@ -166,7 +166,8 @@ class ConnectionCubit extends Cubit<ConnectionState> {
166
166
@override
167
167
Future <void > close () {
168
168
_connectivityTimer? .cancel ();
169
-
169
+ connectivitySubscription? .cancel ();
170
+ connectivitySubscription = null ;
170
171
return super .close ();
171
172
}
172
173
}
0 commit comments