Skip to content

Commit 0e9637f

Browse files
committed
Minor improvements
1 parent 1b934cd commit 0e9637f

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

lib/src/socket.dart

+8-7
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ class PhoenixSocket {
203203
if (reconnect) {
204204
_reconnect(code ?? normalClosure, reason: reason);
205205
} else {
206-
_closeConnection(code ?? goingAway, reason: reason);
206+
_closeConnection(
207+
// Should be goingAway, but https://github.com/dart-lang/http/issues/1294
208+
code ?? normalClosure,
209+
reason: reason,
210+
);
207211
}
208212
}
209213

@@ -407,7 +411,7 @@ class PhoenixSocket {
407411
return false;
408412
} catch (error, stackTrace) {
409413
_logger.warning('Heartbeat message failed', error, stackTrace);
410-
_reconnect(4001, reason: 'Heartbeat timeout');
414+
_reconnect(heartbeatTimedOut, reason: 'Heartbeat timed out');
411415
return false;
412416
}
413417
}
@@ -485,11 +489,8 @@ class PhoenixSocket {
485489
completer.complete(message);
486490
}
487491

488-
if (message.ref != _latestHeartbeatRef) {
489-
// The connection is alive, prevent heartbeat timeout from closing
490-
// connection.
491-
_latestHeartbeatRef = null;
492-
}
492+
// The connection is alive, prevent heartbeat timeout from closing it.
493+
_latestHeartbeatRef = null;
493494
}
494495

495496
if (message.topic != null && message.topic!.isNotEmpty) {

lib/src/socket_connection.dart

+19-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ typedef WebSocketChannelFactory = Future<WebSocketChannel> Function();
1212

1313
final _logger = Logger('phoenix_socket.connection');
1414

15+
// Some custom close codes.
16+
const unknownReason = 4000;
17+
const heartbeatTimedOut = 4001;
18+
const forcedReconnectionRequested = 4002;
19+
1520
/// Maintains connection to the underlying websocket, reconnecting to it if
1621
/// necessary.
1722
class SocketConnectionManager {
@@ -65,7 +70,8 @@ class SocketConnectionManager {
6570
return;
6671
}
6772

68-
_stopConnecting(4002, 'Immediate connection requested');
73+
_stopConnecting(
74+
forcedReconnectionRequested, 'Immediate connection requested');
6975
_connectionAttempts = 0;
7076
}
7177
_maybeConnect();
@@ -167,7 +173,8 @@ class SocketConnectionManager {
167173
} finally {
168174
if (_disposed) {
169175
// 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');
171178
connectionCompleter.completeError(StateError('Client disposed'));
172179
} else if (connectionFuture != _pendingConnection) {
173180
connection?.close(normalClosure, 'Closing obsolete connection');
@@ -176,7 +183,7 @@ class SocketConnectionManager {
176183
connectionCompleter
177184
.completeError(StateError('Connection attempt aborted'));
178185
} else {
179-
// _startConnecting() was called during connection attempt, return the
186+
// _connect() was called during connection attempt, return the
180187
// new Future instead.
181188
connectionCompleter.complete(_pendingConnection);
182189
}
@@ -230,7 +237,11 @@ class SocketConnectionManager {
230237
_onError(error, stackTrace);
231238
}
232239

233-
rethrow;
240+
if (error is ConnectionInitializationException) {
241+
rethrow;
242+
} else {
243+
throw ConnectionInitializationException(error, stackTrace);
244+
}
234245
}
235246
}
236247

@@ -383,7 +394,10 @@ class _WebSocketConnection {
383394
onError: onError,
384395
onDone: () {
385396
onStateChange(
386-
WebSocketDisconnected._(_ws.closeCode ?? 4000, _ws.closeReason),
397+
WebSocketDisconnected._(
398+
_ws.closeCode ?? unknownReason,
399+
_ws.closeReason,
400+
),
387401
);
388402
acceptingMessages = false;
389403
subscription.cancel();

0 commit comments

Comments
 (0)