@@ -12,6 +12,11 @@ typedef WebSocketChannelFactory = Future<WebSocketChannel> Function();
12
12
13
13
final _logger = Logger ('phoenix_socket.connection' );
14
14
15
+ // Some custom close codes.
16
+ const unknownReason = 4000 ;
17
+ const heartbeatTimedOut = 4001 ;
18
+ const forcedReconnectionRequested = 4002 ;
19
+
15
20
/// Maintains connection to the underlying websocket, reconnecting to it if
16
21
/// necessary.
17
22
class SocketConnectionManager {
@@ -65,7 +70,8 @@ class SocketConnectionManager {
65
70
return ;
66
71
}
67
72
68
- _stopConnecting (4002 , 'Immediate connection requested' );
73
+ _stopConnecting (
74
+ forcedReconnectionRequested, 'Immediate connection requested' );
69
75
_connectionAttempts = 0 ;
70
76
}
71
77
_maybeConnect ();
@@ -167,7 +173,8 @@ class SocketConnectionManager {
167
173
} finally {
168
174
if (_disposed) {
169
175
// Manager was disposed while running connection attempt.
170
- connection? .close (goingAway, 'Client disposed' );
176
+ // Should be goingAway, but https://github.com/dart-lang/http/issues/1294
177
+ connection? .close (normalClosure, 'Client disposed' );
171
178
connectionCompleter.completeError (StateError ('Client disposed' ));
172
179
} else if (connectionFuture != _pendingConnection) {
173
180
connection? .close (normalClosure, 'Closing obsolete connection' );
@@ -176,7 +183,7 @@ class SocketConnectionManager {
176
183
connectionCompleter
177
184
.completeError (StateError ('Connection attempt aborted' ));
178
185
} else {
179
- // _startConnecting () was called during connection attempt, return the
186
+ // _connect () was called during connection attempt, return the
180
187
// new Future instead.
181
188
connectionCompleter.complete (_pendingConnection);
182
189
}
@@ -230,7 +237,11 @@ class SocketConnectionManager {
230
237
_onError (error, stackTrace);
231
238
}
232
239
233
- rethrow ;
240
+ if (error is ConnectionInitializationException ) {
241
+ rethrow ;
242
+ } else {
243
+ throw ConnectionInitializationException (error, stackTrace);
244
+ }
234
245
}
235
246
}
236
247
@@ -383,7 +394,10 @@ class _WebSocketConnection {
383
394
onError: onError,
384
395
onDone: () {
385
396
onStateChange (
386
- WebSocketDisconnected ._(_ws.closeCode ?? 4000 , _ws.closeReason),
397
+ WebSocketDisconnected ._(
398
+ _ws.closeCode ?? unknownReason,
399
+ _ws.closeReason,
400
+ ),
387
401
);
388
402
acceptingMessages = false ;
389
403
subscription.cancel ();
0 commit comments