Ejemplo n.º 1
0
        public void SetUp()
        {
            ManualResetEvent evt   = new ManualResetEvent(false);
            INError          error = null;

            client = new NClient.Builder(DefaultServerKey).Build();
            var message = NAuthenticateMessage.Device(DeviceId);

            client.Login(message, (INSession session) =>
            {
                userId = session.Id;
                client.Connect(session);
                evt.Set();
            }, (INError err) => {
                client.Register(message, (INSession session) =>
                {
                    userId = session.Id;
                    client.Connect(session);
                    evt.Set();
                }, (INError err2) => {
                    error = err2;
                    evt.Set();
                });
            });

            evt.WaitOne(1000, false);
            Assert.IsNull(error);
        }
Ejemplo n.º 2
0
        public void RegisterCustom_Invalid([ValueSource("BlankCases")] string id)
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Custom(id);

            client.Register(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            if (id == string.Empty)
            {
                Assert.AreEqual("Custom ID is required", result.Message);
            }
            else
            {
                Assert.AreEqual("Invalid custom ID, no spaces or control characters allowed", result.Message);
            }
        }
Ejemplo n.º 3
0
        // This method connects the client to the server and
        // if neccessary authenticates with the server
        public void Connect(INAuthenticateMessage request)
        {
            // Check to see if we have a valid session token we can restore
            var session = RestoreSession();

            if (session != null)
            {
                // Session is valid, let's connect.
                _sessionHandler(session);
                return;
            }

            // Cache message for later use for reconnecting purposes in case the session had expired
            _authenticateMessage = request;

            // Let's login to authenticate and get a valid token
            _client.Login(request, _sessionHandler, err =>
            {
                if (err.Code == ErrorCode.UserNotFound)
                {
                    _client.Register(request, _sessionHandler, ErrorHandler);
                }
                else
                {
                    ErrorHandler(err);
                }
            });
        }
    private void LoginOrRegister()
    {
        // See if we have a cached id in PlayerPrefs.
        var id = PlayerPrefs.GetString("nk.id");

        if (string.IsNullOrEmpty(id))
        {
            // We'll use device ID for the user account. Have a look at the docs
            // for other options: https://heroiclabs.com/docs/development/authentication/
            id = SystemInfo.deviceUniqueIdentifier;
            // Store the identifier for next game start.
            PlayerPrefs.SetString("nk.id", id);
        }

        var message = NAuthenticateMessage.Device(id);

        _client.Login(message, SessionHandler, (INError error) => {
            if (error.Message.Equals("ID not found"))
            {
                _client.Register(message, SessionHandler, ErrorHandler);
                return;
            }
            Debug.LogErrorFormat("Login error: code '{0}' - '{1}'.", error.Code, error.Message);
        });
    }
Ejemplo n.º 5
0
        public void LoginEmail_Valid()
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INSession        result = null;

            string   piece    = random.GetString().Substring(8);
            string   email    = String.Format("{0}@{0}.com", piece);
            string   password = TestContext.CurrentContext.Random.GetString();
            INClient client   = NClient.Default(DefaultServerKey);
            var      message  = NAuthenticateMessage.Email(email, password);

            client.Register(message, (INSession _) =>
            {
                client.Login(message, (INSession session) =>
                {
                    result = session;
                    evt.Set();
                }, (INError error) =>
                {
                    evt.Set();
                });
            }, (INError error) =>
            {
                evt.Set();
            });

            evt.WaitOne(1000, false);
            Assert.NotNull(result);
            Assert.NotNull(result.Token);
        }
Ejemplo n.º 6
0
        public void LoginDevice_Valid()
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INSession        result = null;

            string   id      = random.GetString();
            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Device(id);

            client.Register(message, (INSession _) =>
            {
                client.Login(message, (INSession session) =>
                {
                    result = session;
                    evt.Set();
                }, (INError error) =>
                {
                    evt.Set();
                });
            }, (INError error) =>
            {
                evt.Set();
            });

            evt.WaitOne(1000, false);
            Assert.NotNull(result);
            Assert.NotNull(result.Token);
        }
    public void ClientRegister()
    {
        if (userNameInputField.text.Length < 8)
        {
            loginErrorString = "Invalid UserName. Must be at least 8 characters!";
            return;
        }
        if (serverIPInputField.text.Length < 11)
        {
            loginErrorString = "Invalid IP!";
            return;
        }

        ServerIP = serverIPInputField.text;

        ClientBuilder();

        deviceID = userNameInputField.text;
        Debug.Log("Logging in as: " + deviceID + " to IP: " + ServerIP);
        PlayerPrefs.SetString("UserName", deviceID);


        ManualResetEvent registerEvent = new ManualResetEvent(false);

        //Register and connect client
        Debug.Log("Trying to register");
        var request = NAuthenticateMessage.Device(deviceID);

        client.Register(request, (INSession session) =>
        {
            this.session           = session;
            ServerInstance.session = session;
            client.Connect(session);
            clientConnected = true;
            NakamaData.Singleton.Session = session;
            registerEvent.Set();
        }, (INError err) =>
        {
            if (err.Code == ErrorCode.AuthError)
            {
                loginErrorString = "This UserName is already registered!";
            }
            else
            {
                loginErrorString = "ID register " + deviceID + " failed: " + err;
            }

            registerEvent.Set();
        });

        registerEvent.WaitOne(1000, false);

        if (clientConnected)
        {
            FetchClientInfo();
            UpdateUserInfo();
            JoinDefaultRoom();
            CompleteLogin();
        }
    }
Ejemplo n.º 8
0
    public void Player2RegisterClick()
    {
        var request = NAuthenticateMessage.Email(EMAIL, PASSWORD);

        client2.Register(request, (INSession session) => {
            Debug.Log("Player 2 Registration successful");
            RegisterButtonPlayer2Enable = false;
        }, (INError error) => {
            Debug.LogErrorFormat("Email register '{0}' failed: {1}", EMAIL, error);
        });
    }
Ejemplo n.º 9
0
    public void Player1RegisterClick()
    {
        string id      = SystemInfo.deviceUniqueIdentifier;
        var    request = NAuthenticateMessage.Device(id);

        client1.Register(request, (INSession session) => {
            Debug.Log("Player 1 Registration successful");
            RegisterButtonPlayer1Enable = false;
        }, (INError error) => {
            Debug.LogErrorFormat("ID register '{0}' failed: {1}", id, error);
        });
    }
Ejemplo n.º 10
0
        // +++ ICustomAuthenticator implementation ++++++++++++++++++++++++++++++++++++++++++++++++++++
        public void Login(object userData, Action <INSession> successCallback, Action <INError> failCallback)
        {
            string id;

            if (userData == null)
            {
                id = System.Guid.NewGuid().ToString();
            }
            else
            {
                if (userData.GetType() != typeof(Guid))
                {
                    throw new ArgumentException("Userdata has to be typeof (Guid).");
                }
                id = ((Guid)userData).ToString();
            }

            var authenticationMessage = NAuthenticateMessage.Device(id);

            _client.Login(authenticationMessage,
                          successCallback,
                          (INError error) =>
            {
                if (error.Code == ErrorCode.UserNotFound)
                {
                    _client.Register(
                        authenticationMessage,
                        successCallback,
                        failCallback
                        );
                }
                else
                {
                    failCallback(error);
                }
            }
                          );
        }
Ejemplo n.º 11
0
        private static INError RegisterEmailError(string email, string password)
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Email(email, password);

            client.Register(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            return(result);
        }
Ejemplo n.º 12
0
        public void RegisterCustom_Valid()
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INSession        result = null;

            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Custom(random.GetString());

            client.Register(message, (INSession session) =>
            {
                result = session;
                evt.Set();
            }, (INError error) =>
            {
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            Assert.NotNull(result.Token);
        }
Ejemplo n.º 13
0
        public void RegisterSteam_Invalid([ValueSource("BlankCases")] string sessionToken)
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Steam(sessionToken);

            client.Register(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            Assert.AreEqual("Steam registration not available", result.Message);
        }
Ejemplo n.º 14
0
        public void SetUp()
        {
            ManualResetEvent evt   = new ManualResetEvent(false);
            INError          error = null;

            client = new NClient.Builder(DefaultServerKey).Build();
            string id      = TestContext.CurrentContext.Random.GetString();
            var    message = NAuthenticateMessage.Device(id);

            client.Register(message, (INSession authenticated) =>
            {
                session = authenticated;
                client.Connect(session);
                evt.Set();
            }, (INError err) => {
                error = err;
                evt.Set();
            });

            evt.WaitOne(1000, false);
            Assert.IsNull(error);
        }
    public void LoginOrRegisterWithCustomId(string customId, Action onLoginSucceeded, Action onLoginError)
    {
        var message = NAuthenticateMessage.Custom(customId);

        _client.Login(message,
                      (INSession session) =>
        {
            Debug.Log("Login Callback");
            SessionHandler(session);
        },
                      (INError err) =>
        {
            if (err.Code == ErrorCode.UserNotFound)
            {
                _client.Register(message, SessionHandler, ErrorHandler);
            }
            else
            {
                ErrorHandler(err);
            }
        }
                      );
    }
Ejemplo n.º 16
0
        public void GetServerTime_Valid()
        {
            ManualResetEvent evt        = new ManualResetEvent(false);
            long             serverTime = 0;

            string   id      = TestContext.CurrentContext.Random.GetString();
            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Device(id);

            client.Register(message, (INSession session) =>
            {
                client.Connect(session);
                Thread.Sleep(500);
                serverTime = client.ServerTime;
                evt.Set();
            }, (INError _) => {
                evt.Set();
            });

            evt.WaitOne(1000, false);
            Assert.That(serverTime, Is.Not.EqualTo(0));
            client.Disconnect();
        }
Ejemplo n.º 17
0
        public void Connect_Valid()
        {
            ManualResetEvent evt = new ManualResetEvent(false);
            var connected        = false;

            string   id      = TestContext.CurrentContext.Random.GetString();
            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Device(id);

            client.Register(message, (INSession session) =>
            {
                client.Connect(session, (bool _) => {
                    connected = true;
                    evt.Set();
                });
            }, (INError _) => {
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.IsTrue(connected);
            client.Disconnect();
        }
Ejemplo n.º 18
0
        public void SetUp()
        {
            INError error = null;

            client1 = new NClient.Builder(DefaultServerKey).Build();
            client2 = new NClient.Builder(DefaultServerKey).Build();

            ManualResetEvent c1Evt = new ManualResetEvent(false);

            client1.Register(NAuthenticateMessage.Device(random.GetString()), (INSession session) =>
            {
                userId1 = session.Id;
                client1.Connect(session);
                c1Evt.Set();
            }, (INError err) =>
            {
                error = err;
                c1Evt.Set();
            });
            c1Evt.WaitOne(5000, false);
            Assert.IsNull(error);

            ManualResetEvent c2Evt = new ManualResetEvent(false);

            client2.Register(NAuthenticateMessage.Device(random.GetString()), (INSession session) =>
            {
                userId2 = session.Id;
                client2.Connect(session);
                c2Evt.Set();
            }, (INError err) =>
            {
                error = err;
                c2Evt.Set();
            });
            c2Evt.WaitOne(5000, false);
            Assert.IsNull(error);
        }
Ejemplo n.º 19
0
    private void RegisterAndConnect()
    {
        // Lets log a message whenever we're disconnected from the server.
        _client.OnDisconnect = (INDisconnectEvent evt) => {
            Debug.Log("Disconnected from the server.");
        };

        // This can be DeviceID, Facebook, Google, GameCenter or a custom ID.
        // For more information have a look at: https://heroiclabs.com/docs/development/user/
        var message = NAuthenticateMessage.Custom(_customUserId);

        _client.Register(message, (INSession session) => {
            Debug.LogFormat("Registered user.");
            // We suggest that you cache the Session object on device storage
            // so you don't have to login each time the game starts.
            // For demo purposes - we'll ignore that.

            _client.Connect(session, (bool connected) => {
                Debug.Log("Socket connected.");
            });
        }, (INError error) => {
            Debug.LogErrorFormat("ID register '{0}' failed: {1}", _customUserId, error);
        });
    }
    void RegisterWithDeviceId()
    {
        var message = NAuthenticateMessage.Device(this.GetId());

        client.Register(message, OnServerSessionCreatedOrRestored, OnError);
    }