Exemplo n.º 1
0
        /// <summary>
        /// Restores session or tries to establish a new one.
        /// Invokes <see cref="ConnectionSucceed"/> or <see cref="ConnectionFailed"/>.
        /// </summary>
        /// <returns></returns>
        public async Task <AuthenticationResponse> ConnectAsync(string username = null)
        {
            var response = await RestoreTokenAsync(username);

            switch (response)
            {
            case AuthenticationResponse.Authenticated:
            case AuthenticationResponse.UserInfoUpdated:
                ConnectionSucceed?.Invoke();
                break;

            case AuthenticationResponse.NewAccountCreated:
                NewAccountCreated?.Invoke();
                ConnectionSucceed?.Invoke();
                break;

            case AuthenticationResponse.ErrorUsernameAlreadyExists:
            case AuthenticationResponse.ErrorInternal:
                ConnectionFailed?.Invoke();
                break;

            default:
                Debug.LogError("Unhandled response received: " + response);
                break;
            }

            return(response);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Restores session or tries to establish a new one.
        /// Invokes <see cref="ConnectionSucceed"/> or <see cref="ConnectionFailed"/>.
        /// </summary>
        /// <returns></returns>
        public async Task <(bool success, string message)> ConnectAsync(string email,
                                                                        string password,
                                                                        bool create = false,
                                                                        string ip   = "localhost",
                                                                        int p       = 7350)
        {
            ipAddress = ip;
            port      = p;
            try
            {
                session = await client.AuthenticateEmailAsync(email, password, create : create);

                account = await GetAccountAsync();
            }
            catch (ApiResponseException ex)
            {
                ConnectionFailed?.Invoke();
                return(false, ex.Message);
            }

            Debug.LogFormat("New user: {0}, {1}", session.Created, session);
            ConnectionSucceed?.Invoke();
            if (session.Created)
            {
                NewAccountCreated?.Invoke();
            }

            return(true, session.Created ? "Account created, connecting ..." : "Connecting");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize the connection to the server.
        /// </summary>
        /// <param name="serverIp">Server IP</param>
        /// <param name="port">Port to connect</param>
        public void Connect(IPAddress serverIp, int port)
        {
            ServerIp = serverIp;
            Port     = port;

            ConnectToServer();
            StartReceivingData();
            ConnectionSucceed?.Invoke(this, EventArgs.Empty);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Handles and fires the <see cref="OnConnected(EventArgs)"/> event.
 /// </summary>
 protected virtual void OnConnected(EventArgs e)
 {
     ConnectionSucceed?.Invoke(this, e);
     Running = true;
     Console.WriteLine("Succesfully connected.");
 }