@@ -222,18 +222,22 @@ class RealtimeClient {
222222 final conn = this .conn;
223223 if (conn != null ) {
224224 final oldState = connState;
225- connState = SocketStates .disconnecting;
226- log ('transport' , 'disconnecting' , {'code' : code, 'reason' : reason},
227- Level .FINE );
225+ final shouldCloseSink =
226+ oldState == SocketStates .open || oldState == SocketStates .connecting;
227+ if (shouldCloseSink) {
228+ // Don't set the state to `disconnecting` if the connection is already closed.
229+ connState = SocketStates .disconnecting;
230+ log ('transport' , 'disconnecting' , {'code' : code, 'reason' : reason},
231+ Level .FINE );
232+ }
228233
229234 // Connection cannot be closed while it's still connecting. Wait for connection to
230235 // be ready and then close it.
231236 if (oldState == SocketStates .connecting) {
232237 await conn.ready.catchError ((_) {});
233238 }
234239
235- if (oldState == SocketStates .open ||
236- oldState == SocketStates .connecting) {
240+ if (shouldCloseSink) {
237241 if (code != null ) {
238242 await conn.sink.close (code, reason ?? '' );
239243 } else {
@@ -319,7 +323,7 @@ class RealtimeClient {
319323 }
320324 }
321325
322- /// Retuns `true` is the connection is open.
326+ /// Returns `true` is the connection is open.
323327 bool get isConnected => connState == SocketStates .open;
324328
325329 /// Removes a subscription from the socket.
0 commit comments