In real-time communications, a user can be in various channel connection states. This page shows how the SDK determines the connection state of a user and transitions between the states.
To help you get the connection state of each user in the channel, the Agora Unity SDK added the OnConnectionStateChangedHandler
callback in v2.3.2. This callback occurs when the connection state of the local user changes. You can get the connection state and reason for the state change in this callback.
With this callback, the SDK provides the following connection states of the users:
DISCONNECTED(1)
: The user is disconnected from the Agora's server.CONNECTING(2)
: The SDK is establishing connections between the user and the server.CONNECTED(3)
: The user is connected to the server.RECONNECTING(4)
: The SDK is trying to reconnect to the the server. This state occurs when the network connection is interrupted, and the SDK automatically tries reconnecting to the server.FAILED(5)
: The connection fails. The SDK stops trying to connect to the server in this state.The following diagram shows how each connection state is defined:
During communications, you can also call the GetConnectionState
method to get the current connection state, and the reason for the state change in the OnJoinChannelSuccessHandler
callback.
When network interruption occurs, the SDK automatically tries reconnecting to the server.
The following diagram shows the callbacks received by UID 1 and UID 2, where UID 1 joins the channel, gets a network exception, loses connection, and rejoins the channel.
Where:
JoinChannelByKey
request from UID 1.JoinChannelByKey
method, UID 1 joins the channel. During the process, UID 1 also receives the OnConnectionStateChangedHandler(CONNECTION_STATE_CONNECTING, CONNECTION_CHANGED_CONNECTING)
callback. When successfully joining the channel, UID 1 receives the OnConnectionStateChangedHandler(CONNECTION_STATE_CONNECTED, CONNECTION_CHANGED_JOIN_SUCCESS)
and OnJoinChannelSuccessHandler
callbacks.OnUserJoinedHandler
callback.OnConnectionStateChangedHandler(CONNECTION_STATE_RECONNECTING, CONNECTION_CHANGED_INTERRUPTED)
callback; meanwhile the SDK continues to try rejoining the channel.OnConnectionStateChangedHandler(CONNECTION_STATE_RECONNECTING, CONNECTION_CHANGED_INTERRUPTED)
, UID 1 receives the OnConnectionLostHandler
callback; meanwhile the SDK continue to try rejoining the channel.OnUserOfflineHandler
.OnConnectionStateChangedHandler(CONNECTION_STATE_RECONNECTING, CONNECTION_CHANGED_INTERRUPTED)
, the SDK stops trying. UID 1 receives the OnConnectionStateChangedHandler(CONNECTION_STATE_FAILED, CONNECTION_CHANGED_JOIN_FAILED)
callback. UID 1 needs to leave the channel and call the JoinChannelByKey
method to join the channel.