connect method

  1. @override
Future<bool> connect(
  1. TcpPrinterInput model
)
override

Implementation

@override
Future<bool> connect(TcpPrinterInput model) async {
  try {
    if (status == TCPStatus.none) {
      _socket = await Socket.connect(model.ipAddress, model.port, timeout: model.timeout);
      status = TCPStatus.connected;
      debugPrint('socket connected'); //if opened you will get it here
      _statusStreamController.add(status);

      // Create ping object with desired args
      final ping = Ping('${model.ipAddress}', interval: 3, timeout: 7);

      // Begin ping process and listen for output
      ping.stream.listen((PingData data) {
        if (data.error != null) {
          debugPrint(' ----- ping error ${data.error}');
          _socket?.destroy();
          status = TCPStatus.none;
          _statusStreamController.add(status);
        }
      });
      listenSocket(ping);
    }
    return true;
  } catch (e) {
    _socket?.destroy();
    status = TCPStatus.none;
    _statusStreamController.add(status);
    return false;
  }
}