Sign in to a game
How to sign in a player to Vivox services.
読み終わるまでの所要時間 2 分最終更新 7ヶ月前
After you initialize the Vivox SDK, you can sign a user in to the Vivox instance assigned to your game by using an account name that is selected by the game.
- Create a one-to-one mapping between an account name and a player.
- Use the same account name for the same player every time they play the game.
- Player account names must be unique for each play and be anonymous.
If you do not want to use a unique account name per player, or if you do not want to expose the username to the network, then use one of the following options:
- A hash of the unique username
- A universally unique identifier (UUID)
The following criteria applies to account names:
- The total length of the issuer + account name cannot exceed 124 characters.
- Can use only the following characters:
-
Alphanumerics0-9, A-Z, a-z0x30-0x39, 0x41-0x5A, 0x61-0x7AExclamation mark' ! '0x21Open parenthesis' ( '0x28Close parenthesis' ) '0x29Plus' + '0x2BMinus' - '0x2DPeriod' . '0x2EEquals' = '0x3DUnderscore' _ '0x5FTilde' ~ '0x7EPercent' % '0x7F
Each participant must have a unique username portion of the AccountId, or collisions can occur during gameplay. If a duplicate AccountId is used in a channel where that AccountId already exists, then the AccountId that is already in channel is kicked from the channel.
The game uses the method ILoginSession::BeginLogin to sign users in to Vivox. It takes as its last argument a delegate that lets you call a callback function after sign in is complete. Note that every sign in method call must have an access token that authorizes that game instance to sign in as the specified account.
For more information, refer to the Access Token Developer Guide.
The following code displays an example of how to initiate the sign in process and how to handle the delegate callback:
/* . . . */AccountId Account = AccountId(kDefaultIssuer, "example_user", kDefaultDomain);ILoginSession &MyLoginSession(MyVoiceClient->GetLoginSession(Account));bool IsLoggedIn = false;// Setup the delegate to execute when login completesILoginSession::FOnBeginLoginCompletedDelegate OnBeginLoginCompleted;OnBeginLoginCompleted.BindLambda([this, &IsLoggedIn, &MyLoginSession](VivoxCoreError Error){ if (VxErrorSuccess == Error) { IsLoggedIn = true; // This bool is only illustrative. The user is now logged in. }});// Request the user to login to VivoxMyLoginSession.BeginLogin(kDefaultServer, MyLoginSession.GetLoginToken(kDefaultKey, kDefaultExpiration), OnBeginLoginCompleted);/* . . . */
In addition to registering an ILoginSession::FOnBeginLoginCompletedDelegate callback, the game must handle ILoginSession::EventStateChanged events that have the state value set to LoginState::LoggedOut. The Vivox SDK sends this message when whenever a sign out occurs.
When a sign out is initiated by a user, LoginState will transition through LoginState::LoggedIn to LoginState::LoggingOut to LoginState::LoggedOut. In the event of a sign out initiated by the server, or due to network loss, the LoggingOut state is skipped and the LoggedIn state changes directly to LoggedOut.
The following code displays an example of this message:
void UMyGameClass::OnLoginSessionStateChanged(LoginState State){ if (LoginState::LoggedOut == State) { UE_LOG(MyLog, Error, TEXT("LoginSession Logged Out Unexpectedly\n")); // Optionally handle other cases }}