文档

支持

Vivox Core SDK

Vivox Core SDK

Sign in to a game

How to sign in a player to Vivox services.
阅读时间2 分钟最后更新于 12 天前

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. For the account name:
  1. Create a one-to-one mapping between an account name and a player.
  2. Use the same account name for the same player every time they play the game.
  3. Player account names must be unique for each play and be anonymous.
The following criteria applies to account names:
  • Maximum of 127 bytes
  • Must start and end with a period (.)
  • No adjacent spaces
  • Can use only the following characters:
  • Alphanumerics

    0-9, A-Z, a-z

    0x30-0x39, 0x41-0x5A, 0x61-0x7A

    Exclamation mark' ! '0x21
    Open parenthesis' ( '0x28
    Close parenthesis' ) '0x29
    Plus' + '0x2B
    Minus' - '0x2D
    Period' . '0x2E
    Equals' = '0x3D
    Underscore' _ '0x5F
    Tilde' ~ '0x7E
    Percent' % '0x7F
The game uses the
vx_req_account_anonymous_login
message to sign users in to Vivox. After the game receives a successful
vx_resp_account_anonymous_login
message, the user is signed in.
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 game sets a display name for the account by using UTF-8 encoding. This name is visible to all users in the channel; they receive it as the displayname field in the
vx_evt_buddy_presence
and
vx_evt_participant_added
events.
  • The display name is only valid for the current session and does not persist in the Vivox network.
  • The maximum display name length is 127 bytes, plus a null terminator.
  • The display name is set in the
    displayname
    field of the
    vx_req_account_anonymous_login
    request.
The following code is an example of how to initiate the sign in process:
. . .vx_req_account_anonymous_login_t *req;vx_req_account_anonymous_login_create(&req);req->connector_handle = vx_strdup("c1");req->acct_name = vx_strdup(".issuer-w-dev.mytestaccountname.");req->displayname = vx_strdup("John Doe");req->account_handle = vx_strdup(req->acct_name);req->access_token = vx_strdup(_the_access_token_generated_by_the_game_server);vx_issue_request3(&req->base, &request_count);. . .
The following code is an example of how to handle the sign in response:
void HandleLoginResponse(vx_resp_account_anonymous_login *resp){ if (resp->base.return_code == 1) { printf(U, resp->base.status_code, vx_get_error_string(resp->base.status_code)); return; } vx_req_account_anonymous_login *req = reinterpret_cast<vx_req_account_anonymous_login *>(req->base.request); printf("login succeeded for account %s\n", req->acct_name);}
In addition to handling the
vx_resp_account_anonymous_login
message, the game must also handle
vx_evt_account_login_state_change
events that have the state field set to
login_state_logged_out
. The Vivox SDK sends this message when the connection to Vivox is lost unexpectedly (such as from network connectivity issues).
The following code is an example of the connection state change messaging process:
void HandleLoginStateChange(vx_evt_account_login_state_change *evt){ if (evt->state == login_state_logged_in) { printf("%s is logged in\n", evt->account_handle); } else if (evt->state == login_state_logged_out) { if (evt->status_code !=0) { printf("%s logged out with status %d:%s\n", evt->status_code, vx_get_error_string(evt->status_code)); } else { printf("%s is logged out\n", evt->acct_name); } }}
After the game receives the
vx_resp_connector_create
message, the game can sign in to Vivox. For more information, refer to Create the connector object.