disconnectWithCallBack method

void disconnectWithCallBack(
  1. OnCloseCallback? closeCallback
)

Closes the socket connection and provides a callback upon completion.

This method logs the user out and terminates the WebSocket connection. The closeCallback is invoked when the disconnection is complete.

Implementation

void disconnectWithCallBack(OnCloseCallback? closeCallback) {
  _invalidateGatewayResponseTimer();
  _resetGatewayCounters();
  // Cancel any pending answer timeout
  _cancelPendingAnswerTimeout();
  clearPushMetaData();
  GlobalLogger().i('disconnect()');
  if (_closed) {
    GlobalLogger().i('WebSocket is already closed');
    closeCallback?.call(0, 'Client send disconnect');
    return;
  }
  // Don't wait for the WebSocket 'close' event, do it now.
  _closed = true;
  _connected = false;
  _registered = false;
  try {
    txSocket.close();
    Future.delayed(const Duration(milliseconds: 100), () {
      closeCallback?.call(0, 'Client send disconnect');
    });
  } catch (error) {
    GlobalLogger().e('close() | error closing the WebSocket: $error');
  }
}