connect method

Future<NetworkPrintResult> connect({
  1. Duration? timeout,
})

Connect to network printer with improved error handling

Implementation

Future<NetworkPrintResult> connect({
  Duration? timeout,
}) async {
  if (_isConnected && _socket != null) {
    return NetworkPrintResult.success;
  }

  try {
    _socket = await Socket.connect(
      _host,
      _port,
      timeout: timeout ?? _timeout,
    );
    _isConnected = true;
    return NetworkPrintResult.success;
  } on SocketException {
    _isConnected = false;
    return NetworkPrintResult.timeout;
  } catch (e) {
    _isConnected = false;
    return NetworkPrintResult.timeout;
  }
}