disconnect method

Future<void> disconnect()

Disconnect from channel.

チャンネルから切断します。

Implementation

Future<void> disconnect() async {
  if (_disconnectingCompleter != null) {
    return disconnecting;
  }
  if (_connectingCompleter != null || !connected) {
    return;
  }
  _disconnectingCompleter = Completer<void>();
  int retried = 0;
  ConnectionStateType? connectionState;
  try {
    if (_engine == null) {
      throw Exception(
        "The engine is not initialized. [connect] the engine first.",
      );
    }
    do {
      _engine?.leaveChannel();
      await _disconnectingCompleter?.future;
      connectionState = await _engine?.getConnectionState();
      if (connectionState !=
          ConnectionStateType.connectionStateDisconnected) {
        retried++;
        if (retried > 3) {
          throw Exception("Failed to disconnect to the channel.");
        }
      }
    } while (
        connectionState != ConnectionStateType.connectionStateDisconnected);
    await _release();
    notifyListeners();
    _disconnectingCompleter?.complete();
    _disconnectingCompleter = null;
    _lastDisconnectedTime = DateTime.now();
  } catch (e) {
    _disconnectingCompleter?.completeError(e);
    _disconnectingCompleter = null;
    rethrow;
  } finally {
    _disconnectingCompleter?.complete();
    _disconnectingCompleter = null;
  }
}