connect method

Future connect()

Implementation

Future connect() async {
  if (uri == null || port == null || uid == null) {
    throw IOException;
  }
  Socket.connect(uri, port!).then((Socket sock) {
    sock.listen((data) async {
      int l = (data[1] & 0xff);
      int h = (data[2] & 0xff);
      int length = (l | h << 8);
      if (data[0] == PING_TYPE) {
        sendPong();
      } else if (data[0] == REPLY_BODY) {
        var message = data.sublist(3, length + 3);
        replybody.Model info = replybody.Model();
        info.mergeFromBuffer(message);
      } else if (data[0] == Message_TYPE) {
        var message = data.sublist(3, length + 3);
        messages.Model model = messages.Model();
        model.mergeFromBuffer(message);
        onMessageReceived(model);
      }
    }, onError: (error, StackTrace trace) {
      socket = null;
      if (!timer.isActive()) {
        timer.setTotalTime(12000);
        timer.startCountDown();
      }
      onConnectionStatusChanged(false);
    }, onDone: () {
      socket = null;
      if (endCode != "999") {
        timer.setTotalTime(12000);
        timer.startCountDown();
      }
      onConnectionStatusChanged(false);
    }, cancelOnError: true);
    socket = sock;
    sendLoginMsg();
  }).catchError((e) {
    socket = null;
    timer.setTotalTime(12000);
    timer.startCountDown();
    onConnectionStatusChanged(false);
  });
}