disconnect method

Future<void> disconnect()

Closes the WebSocket connection.

This method gracefully closes the WebSocket connection if it is open. Prints a message to the console when the connection is closed or if there was no active connection.

Implementation

Future<void> disconnect() async {
  if (_socket != null) {
    await _socket!.close(); // Close the WebSocket connection.
    debugPrint('Disconnected from WebSocket server.');
  } else {
    debugPrint('No connection to close.');
  }
}