connect method

AsyncReply<bool> connect({
  1. AuthenticationMethod method = AuthenticationMethod.None,
  2. ISocket? socket,
  3. String? hostname,
  4. int? port,
  5. String? username,
  6. int? tokenIndex,
  7. DC? passwordOrToken,
  8. String? domain,
  9. bool useWebsocket = false,
  10. bool secureWebSocket = false,
})

Implementation

AsyncReply<bool> connect(
    {AuthenticationMethod method = AuthenticationMethod.None,
    ISocket? socket,
    String? hostname,
    int? port,
    String? username,
    int? tokenIndex,
    DC? passwordOrToken,
    String? domain,
    bool useWebsocket = false,
    bool secureWebSocket = false}) {
  if (_openReply != null)
    throw AsyncException(ErrorType.Exception, 0, "Connection in progress");

  _openReply = new AsyncReply<bool>();

  if (hostname != null) {
    _session =
        new Session(new ClientAuthentication(), new HostAuthentication());

    _session?.localAuthentication.method = method;
    _session?.localAuthentication.tokenIndex = tokenIndex;
    _session?.localAuthentication.domain = domain;
    _session?.localAuthentication.username = username;
    _localPasswordOrToken = passwordOrToken;
  }

  if (_session == null)
    throw AsyncException(ErrorType.Exception, 0, "Session not initialized");

  if (socket == null) {
    if (useWebsocket) {
      socket = new WSocket()..secure = secureWebSocket;
    } else
      socket = new TCPSocket();
  }

  _port = port ?? _port;
  _hostname = hostname ?? _hostname;

  if (_hostname == null) throw Exception("Host not specified.");

  if (socket != null) {
    socket.connect(_hostname as String, _port).then<dynamic>((x) {
      assign(socket as ISocket);
    }).error((x) {
      _openReply?.triggerError(x);
      _openReply = null;
    });
  }

  return _openReply as AsyncReply<bool>;
}