// +++ unity callbacks ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ async void Start() { // Init List of connected users _connectedUsers = new List <IUserPresence>(0); // get the nakama client _client = new Client("defaultkey", nvpGameManager.HOST, nvpGameManager.PORT, false); // use random guid as device id (for local testing) _id = System.Guid.NewGuid().ToString(); // request a authenticated session from the server and // authenticate the user with either the random device id // or email/password if (nvpGameManager.UNIQUEID != string.Empty) { _session = await _client.AuthenticateDeviceAsync(nvpGameManager.UNIQUEID); } else { _session = await _client.AuthenticateEmailAsync(nvpGameManager.EMAIL, nvpGameManager.PASSWORD); } _account = await _client.GetAccountAsync(_session); this.OnAccountLoaded(this, _account); }
async void EstablishConnection() { var client = new Client("http", "127.0.0.1", 7350, "defaultkey"); // Restore. var authtoken = PlayerPrefs.GetString("nakama.session"); session = Session.Restore(authtoken); try { account = await client.GetAccountAsync(session); } catch (System.Exception e) { Debug.Log(e.Message); var deviceId = SystemInfo.deviceUniqueIdentifier; session = await client.AuthenticateDeviceAsync(deviceId); account = await client.GetAccountAsync(session); PlayerPrefs.SetString("nakama.session", session.AuthToken); } Debug.Log($"User ID = {session.UserId}"); // "ea1e7609-372a-4d67-a495-58f955f3328b" Debug.Log($"UserName = {session.Username}"); // "wRkuUTbKmY" Debug.Log($"Accout User ID = {account.User.Id}"); // "ea1e7609-372a-4d67-a495-58f955f3328b" }
public void Init(IClient client, ISocket socket, IApiAccount account, ISession session) { _client = client; _socket = socket; Account = account; Session = session; }
/// <summary> /// Receives currently logged in user's <see cref="IApiAccount"/> from server. /// </summary> public async Task <IApiAccount> GetAccountAsync() { try { IApiAccount results = await Client.GetAccountAsync(Session); return(results); } catch (Exception e) { Debug.LogWarning("An error has occured while retrieving account: " + e); return(null); } }
/// <summary> /// Retrieves gold count owned by the user and sets the value in UI. /// </summary> private async Task UpdateFundsCounterAsync() { IApiAccount account = await NakamaSessionManager.Instance.GetAccountAsync(); string wallet = account.Wallet; try { Dictionary <string, int> currency = wallet.FromJson <Dictionary <string, int> >(); _funds = currency["gold"]; } catch (Exception) { } _fundsText.text = _funds.ToString(); _buyRandomCardButton.interactable = _funds >= 50; }
// +++ unity lifecycle ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ async void Start() { // register ui events _ui.OnCreateMatch += OnUiCreateMatch; _ui.OnJoinMatch += OnUiJoinMatch; _ui.OnStartGame += OnStartGame; // start nakama client _client = new Client("defaultkey", _host, _port, false); // const string email = "*****@*****.**"; // const string password = "******"; // _session = await _client.AuthenticateEmailAsync(email, password); _session = await _client.AuthenticateDeviceAsync(System.Guid.NewGuid().ToString()); Debug.LogFormat("Authenticated session: {0}", _session); Debug.Log(_session.AuthToken); // raw JWT token Debug.LogFormat("User id '{0}'", _session.UserId); Debug.LogFormat("User username '{0}'", _session.Username); Debug.LogFormat("Session has expired: {0}", _session.IsExpired); Debug.LogFormat("Session expires at: {0}", _session.ExpireTime); // in seconds. _account = await _client.GetAccountAsync(_session); Debug.LogFormat("User id '{0}'", _account.User.Id); Debug.LogFormat("User username '{0}'", _account.User.Username); Debug.LogFormat("Account virtual wallet '{0}'", _account.Wallet); _socket = _client.CreateWebSocket(); _socket.OnConnect += _socket_OnConnect; _socket.OnDisconnect += _socket_OnDisconnect; _socket.OnChannelMessage += _socket_OnChannelMessage; _socket.OnChannelPresence += _socket_OnChannelPresence; _socket.OnError += _socket_OnError; _socket.OnMatchmakerMatched += _socket_OnMatchmakerMatched; _socket.OnMatchPresence += _socket_OnMatchPresence; _socket.OnMatchState += _socket_OnMatchState; _socket.OnNotification += _socket_OnNotification; _socket.OnStatusPresence += _socket_OnStatusPresence; _socket.OnStreamPresence += _socket_OnStreamPresence; _socket.OnStreamState += _socket_OnStreamState; await _socket.ConnectAsync(_session); }
// +++ event handler ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void OnAccountLoaded(object s, IApiAccount account) { _userName.text = account.User.DisplayName; }
private async Task InitializeGame(string deviceId) { var client = new Client("http", "localhost", 7350, "defaultkey", UnityWebRequestAdapter.Instance); client.Timeout = 5; var socket = client.NewSocket(useMainThread: true); string authToken = PlayerPrefs.GetString(GameConstants.AuthTokenKey, null); bool isAuthToken = !string.IsNullOrEmpty(authToken); string refreshToken = PlayerPrefs.GetString(GameConstants.RefreshTokenKey, null); ISession session = null; // refresh token can be null/empty for initial migration of client to using refresh tokens. if (isAuthToken) { session = Session.Restore(authToken, refreshToken); // Check whether a session is close to expiry. if (session.HasExpired(DateTime.UtcNow.AddDays(1))) { try { // get a new access token session = await client.SessionRefreshAsync(session); } catch (ApiResponseException) { // get a new refresh token session = await client.AuthenticateDeviceAsync(deviceId); PlayerPrefs.SetString(GameConstants.RefreshTokenKey, session.RefreshToken); } PlayerPrefs.SetString(GameConstants.AuthTokenKey, session.AuthToken); } } else { session = await client.AuthenticateDeviceAsync(deviceId); PlayerPrefs.SetString(GameConstants.AuthTokenKey, session.AuthToken); PlayerPrefs.SetString(GameConstants.RefreshTokenKey, session.RefreshToken); } try { await socket.ConnectAsync(session); } catch (Exception e) { Debug.LogWarning("Error connecting socket: " + e.Message); } IApiAccount account = null; try { account = await client.GetAccountAsync(session); } catch (ApiResponseException e) { Debug.LogError("Error getting user account: " + e.Message); } _connection.Init(client, socket, account, session); }