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); }
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(); } }
public void Player1Connect() { client1.Connect(session1); Debug.Log("Player 1 connected successfully"); client1Connected = true; ConnectPlayer1Enable = false; }
public void Player2Connect() { client2.Connect(session2); Debug.Log("Player 2 connected successfully"); ConnectPlayer2Enable = false; SelfPlayer2Enable = true; client1Connected = true; }
public void Player2Connect() { client2.Connect(session2); Debug.Log("Player 2 connected successfully"); client2Connected = true; ConnectPlayer2Enable = false; JoinTopicPlayer1Enable = true; }
private void SessionHandler(INSession session) { Debug.LogFormat("Session: '{0}'.", session.Token); _session = session; _client.Connect(_session, (bool done) => { Debug.Log("Session connected."); // We'll set a UI label from the socket. _uiLabelMessage = "Connected."; // Store session for quick reconnects. PlayerPrefs.SetString("nk.session", session.Token); }); }
private NakamaManager() { _client = new NClient.Builder(ServerKey).Host(HostIp).Port(Port).SSL(UseSsl).Build(); _client.OnTopicMessage = message => { var chatMessages = StateManager.Instance.ChatMessages; foreach (var topic in chatMessages.Keys) { if (topic.Id.Equals(message.Topic.Id)) { chatMessages[topic].Add(message.MessageId, message); } } }; _sessionHandler = session => { Logger.LogFormat("Session: '{0}'.", session.Token); _client.Connect(session, done => { Session = session; _reconnectCount = 0; // cache session for quick reconnects _dispatchQueue.Enqueue(() => { PlayerPrefs.SetString("nk.session", session.Token); AfterConnected(this, EventArgs.Empty); }); }); }; _client.OnDisconnect = evt => { Logger.Log("Disconnected from server."); if (_doReconnect && _reconnectCount < MaxReconnectAttempts) { _reconnectCount++; _dispatchQueue.Enqueue(() => { Reconnect(); }); } else { _dispatchQueue.Clear(); _dispatchQueue.Enqueue(() => { AfterDisconnected(this, EventArgs.Empty); }); } }; _client.OnError = error => ErrorHandler(error); }
private void SessionHandler(INSession session) { _session = session; Debug.LogFormat("Session: '{0}'.", session.Token); _client.Connect(_session, (bool done) => { // We enqueue callbacks which contain code which must be dispatched on // the Unity main thread. // foreach(Action a in OnSessionConnectedActions) { // a(); // } Enqueue(() => { Debug.Log("Session connected."); // Store session for quick reconnects. onSessionConnected(session); PlayerPrefs.SetString("nk.session", session.Token); }); }); }
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); }
// +++ functions ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public INClient LoginOrRegister(string loginType, object userData) { _client = new NClient .Builder(serverSettings.serverKey) .Host(serverSettings.hostName) .Port(serverSettings.port) .SSL(serverSettings.ssl) .Build(); // register a central point to handle events change the consistence of the // players in the match _client.OnMatchPresence += OnMatchPresence; AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory(_client); ICustomAuthenticator authenticator = authenticatorFactory.GetAuthenticator(loginType); authenticator.Login( userData, (INSession session) => { _client.Connect(session, (bool done) => { if (OnConnected != null) { OnConnected(session); } }); }, (INError error) => { if (OnConnected != null) { OnConnected(null); } } ); return(_client); }
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(); }
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(); }
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); }
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 OnServerSessionCreatedOrRestored(INSession session) { Debug.Log("Session created or restored"); client.Connect(session, OnConnected); }