initialize method

Future<bool> initialize({
  1. required void rh(
    1. CuppsBase,
    2. String
    ),
  2. required void onClose(),
  3. required void onLost(),
  4. required String logID,
  5. required String logSuff,
})

Implementation

Future<bool> initialize({
  // required String ip,
  // required int port,
  required void Function(CuppsBase, String) rh,
  required void Function() onClose,
  required void Function() onLost,
  required String logID,
  required String logSuff,
}) async {
  // if (_socket != null) {
  //   await _socket!.close();
  // }
  messageID = 1;
  requestHandler = rh;
  onConnectionClose = onClose;
  logIdentifier = logID;
  logSuffix = logSuff;
  onConnectionLost = onLost;
  try {
    // late RawSocketOption option;
    // if (Platform.isAndroid) {
    //   option = RawSocketOption.fromBool(/* SOL_SOCKET */ 0x1, /* SO_KEEPALIVE */ 0x0009, true);
    // } else {
    //   // option = RawSocketOption.fromBool(/* SOL_SOCKET */ 0xffff, /* SO_KEEPALIVE */ 0x0008, true);
    //   option = RawSocketOption.fromInt(/* SOL_SOCKET */ 0xffff, /* SO_KEEPALIVE */ 0x0008, 5);
    // }
    log("Reconnecting socket");
    Socket s = await Socket.connect(ip, port, timeout: const Duration(seconds: 10)).catchError((e) {
      throw Exception("$e");
    }).timeout(const Duration(seconds: 12), onTimeout: () {
      throw Exception("TimeOut");
    });

    await Logger.generalLog("$logIdentifier Socked Connected -> $ip : $port");
    s.listen(_onSocketMessage, cancelOnError: true, onError: (e) async {
      log("ERROR ON SOCKET $e");
      await Logger.generalLog("$logIdentifier Socked Disconnected due to Error -> $ip : $port");
      disconnect();
    }, onDone: () async {
      log("Socket DONE");
      await Logger.generalLog("$logIdentifier Socked Disconnected due to Finish -> $ip : $port");
      disconnect();
    });
    log("Socket set");
    enableRetry();
    _socket = s;
    return true;
  } catch (e) {
    log("ERROR ON LISTENING TO SOCKET $e");
    return false;
  }
}