dispose method

Future<void> dispose()

Closes the underlying socket if connected, and stops reconnection attempts. After calling this method, this SocketClient instance must be considered unusable. Instead, create a new instance of this class.

Use this method if you'd like to disconnect from the specified server permanently, and you'd like to connect to another server instead of the current one.

Implementation

Future<void> dispose() async {
  // Make sure we do not attempt to reconnect when we close the socket
  // and onConnectionLost is called (as part of onDone)
  _wasDisposed = true;
  print('Disposing socket client..');
  _reconnectTimer?.cancel();
  _pingTimer?.cancel();
  _keepAliveSubscription?.cancel();

  await Future.wait([
    _closeSocketChannel(),
    _messageSubscription?.cancel(),
    _connectionStateController.close(),
  ].where((future) => future != null).cast<Future<dynamic>>().toList());
}