This article reviews how to create reconnection logic for application suspension and unexpected disconnects.
The Vivox SDK does not have a built-in method of reconnecting if an application unexpectedly disconnects or when an application resumes after being suspended.
The following methods of reconnection require you to store and maintain the following user information:
- Store
- A list of connected channels
- Any configurations adjusted on each of the channels (for example, Name, Audio Enabled, Text Enabled, and Transmitting)
- The player’s username that is used in the AccountID.
- Update
- When joining a new channel, add the channel to your list
- When leaving a channel, remove the channel from your list
When you are able to store and maintain this data, you can implement your reconnection logic.
The following sections detail methods for handling unexpected disconnections and for handling application suspension.
Unexpected disconnections
- When the user has been unexpectedly disconnected from the internet, the SDK signs out the user from Vivox services.
- Monitor your sign out events with an event handler:
- Unity/Core: Ensure that you add a member bool to monitor whether you are intentionally signing out the user or the SDK is signing them out. Set the bool to false on sign in and set to true on user initiated sign out. When your signed out event handler fires, ensure that it checks your bool. If it is true, sign out as expected. If it is false, attempt to sign back in.
- Unreal: A member bool is not necessary; you only receive a sign out event when you are signed out unexpectedly.
- If your first attempt to sign back in fails, try to sign in again with a timeout.
- For example, wait five seconds before attempting to sign in again to ensure that you are not hammering the server with server calls.
- Note: Ensure that you store your sign in information so you know what data to use when trying to sign the user back in.
- If your second attempt to sign back in fails, try to sign in again with a timeout.
- For example, wait 15 seconds before attempting to sign in again to ensure you are not hammering the server with server calls.
- If you find that your application is in an extreme scenario, such as the SDK is no longer responsive, then uninitialize the SDK and then initialize before you try to sign back in again.
- After sign in is successful, rejoin any channels the user was previously connected to.
- Ensure that you store any information regarding the channels that the user was in, as well as local settings, which can include mute states, volume, and devices being used.
- Iterate through your list of channels and join with the stored configurations.
- Ensure that you check your game logic before joining channels. For example, do not join a channel if a match has ended.
Application suspension
- Store the following information:
- Client details, such as your player’s username
- The channels that the user was in
- Local settings, such as mute states, volume, and devices being used
- Sign out on application suspension.
- If possible, sign out before suspension to prevent errors, if you can detect it’s occurring.
- If not, sign out of voice after your application returns from suspension.
- After you have returned from suspension and are signed out, sign in again as the same user by using your stored information.
- Rejoin channels with stored information.
- Iterate through your list of channels and join with their configurations (for example, Channel Name and Audio Enabled).
- Ensure that you check your game logic before joining channels. For example, do not join a channel if a match has ended.
Note: For Unity implementations, detect application suspension with OnApplicationFocus() or OnApplicationPause(), depending on your needs.
To learn more about how these functions work and when to use each function, see the Unity API Documentation.
Maintaining voice when backgrounding on mobile
When an app is backgrounded on a mobile device, voice typically still works for a period of time. However, on both Android and iOS, at some point after the application is backgrounded, the device reclaims the memory that is being used and you are disconnected from voice.
If you want voice to be continuously connected, you might be able to accomplish this by linking your media player to a separate activity. This has not been tested, but based on Vivox research, having a separate plugin manage the media player can allow you to foreground voice, even when the rest of the game is backgrounded. This also prevents you from being disconnected when your app is backgrounded.
Important notes
- When performing actions such as signing in or rejoining channels, ensure that you generate new Vivox Access Tokens (VATs). Reusing a VAT can cause errors.
- When applications are being suspended, sometimes the OS forces the application into suspension if the cleanup process is taking too long.
- Uninitializing automatically disconnects and signs out the user (if you haven’t already performed this action).
Comments
0 comments
Article is closed for comments.