disconnect method

  1. @override
Future disconnect({
  1. Duration? timeout,
})
override

disconnect flushes pending data and closes the socket.

timeout — optional delay after closing (useful for printers that need time to finish processing before a reconnect).

Implementation

@override
Future disconnect({Duration? timeout}) async {
  final s = socket;
  socket = null;
  try {
    if (s != null && _socketError == null) {
      await s.flush().timeout(const Duration(seconds: 3));
      await s.close().timeout(const Duration(seconds: 3));
    }
  } catch (_) {
    // Ignore errors during cleanup.
  } finally {
    try {
      await _sub?.cancel();
    } catch (_) {}
    _sub = null;
    s?.destroy();
    _socketError = null;
    _rx.clear();
    final w = _rxWaiter;
    if (w != null && !w.isCompleted) w.complete();
    _rxWaiter = null;
  }

  if (timeout != null) {
    await Future.delayed(timeout);
  }
}