Before a user uses ZEGOCLOUD Express SDK to make audio and video calls or perform live streaming, the user must join a room to receive callback notifications about events such as adding or removing streams by other users in the same room, and user login and logout events. Therefore, a connection state of a user in the room determines whether the user can normally use the audio and video services.
This topic describes how to determine a connection state of a user in a room. Further, this topic describes the transition process of connection states.
A user can monitor the connection state of the user in the current room in real time by listening for the onRoomStateChanged
callback. If the connection state of the user changes, the SDK triggers the callback, and the current connection state and related error code are returned in the callback.
The SDK will retry internally if the connection to the room is interrupted due to poor network quality.
The room connection states flow. The developer can determine a state based on the returned reason and handle corresponding logic based on the return value of the errorCode
parameter.
State | Description | Common trigger event |
---|---|---|
ZegoRoomStateChangedReason.LOGINING |
A user is logging in to a room. This state is entered when a user calls the loginRoom operation to log in to a room or calls the switchRoom operation to switch to another room. This state indicates that a request is being initiated to connect to the server. On the application UI, the state of logging in to the room is displayed. |
|
ZegoRoomStateChangedReason.LOGINED |
A user has successfully logged in to a room. This state is entered when a user has successfully logged in to a room or switched to another room. In this state, the user can receive callback notifications about other users and stream additions or deletions in the room. |
|
ZegoRoomStateChangedReason.LOGIN_FAILED |
A user fails to log in to a room. This state is entered when a user fails to log in to a room or fails to switch to another room due to a reason such as an invalid AppID , AppSign , or Token parameter. |
|
ZegoRoomStateChangedReason.RECONNECTING |
The room connection is temporarily interrupted. The SDK will retry internally if the interruption is caused by poor network quality. |
The room connection is temporarily interrupted due to poor network quality, and reconnection is triggered. In this case, the return value of the errorCode parameter is 1002051 . |
ZegoRoomStateChangedReason.RECONNECTED |
Room reconnection succeeds. The SDK will retry internally if the interruption is caused by poor network quality. If the reconnection succeeds, this state is entered. |
Reconnection succeeds after the room connection is temporarily interrupted due to poor network quality. In this case, the return value of the errorCode parameter is 0 . |
ZegoRoomStateChangedReason.RECONNECT_FAILED |
Room reconnection fails. The SDK will retry internally if the interruption is caused by poor network quality. If the reconnection fails, this state is entered. |
Reconnection fails after the room connection is temporarily interrupted due to poor network quality. In this case, the return value of the errorCode parameter is 1002053 . |
ZegoRoomStateChangedReason.KICK_OUT |
The server forces a user to log out of a room. For example, this state is entered when a user is forced to log out of a room because a user with the same name has already logged in to the room on another platform. |
|
ZegoRoomStateChangedReason.LOGOUT |
A user has successfully logged out of a room. This is the default state before a user logs in to a room. If the user has successfully logged out of a room by calling the logoutRoom or the SDK has successfully logged out of the current room internally when the switchRoom operation is called, this state is also entered. |
|
ZegoRoomStateChangedReason.LOGOUT_FAILED |
A user fails to log out of a room. This state is entered if a user fails to log out of a room by calling the logoutRoom operation, or if the SDK fails to log out of the current room when the switchRoom operation is called. |
The room ID does not exist when a user calls the logoutRoom operation to log out of a room in a multi-room scenario. In this case, the return value of the errorCode parameter is 1002002 . |
You can refer to the following code to handle common business events:
-(void)onRoomStateChanged:(ZegoRoomStateChangedReason)reason errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData roomID:(NSString *)roomID {
if(reason == ZegoRoomStateChangedReasonLogining)
{
// logging in
// When loginRoom is called to log in to the room or switchRoom is used to switch to the target room, it enters this state, indicating that it is requesting to connect to the server.
}
else if(reason == ZegoRoomStateChangedReasonLogined)
{
// login successful
// Currently, the loginRoom is successfully called by the developer or the callback triggered by the successful switchRoom. Here, you can handle the business logic for the first successful login to the room, such as fetching chat room and live streaming basic information.
}
else if(reason == ZegoRoomStateChangedReasonLoginFailed)
{
// login failed
if (errorCode == 1002033) {
//When using the login room authentication function, the incoming Token is incorrect or expired.
}
}
else if(reason == ZegoRoomStateChangedReasonReconnecting)
{
// Reconnecting
// This is currently a callback triggered by successful disconnection and reconnection of the SDK. It is recommended to show some reconnection UI here.
}
else if(reason == ZegoRoomStateChangedReasonReconnected)
{
// Reconnection successful
}
else if(reason == ZegoRoomStateChangedReasonReconnectFailed)
{
// Reconnect failed
// When the room connection is completely disconnected, the SDK will not reconnect. If developers need to log in to the room again, they can actively call the loginRoom interface.
// At this time, you can exit the room/live broadcast room/classroom in the business, or manually call the interface to log in again.
}
else if(reason == ZegoRoomStateChangedReasonKickOut)
{
// kicked out of the room
if (errorCode == 1002050) {
// The user was kicked out of the room (because the user with the same userID logged in elsewhere).
}
else if (errorCode == 1002055) {
// The user is kicked out of the room (the developer actively calls the background kicking interface).
}
}
else if(reason == ZegoRoomStateChangedReasonLogout)
{
// Logout successful
// The developer actively calls logoutRoom to successfully log out of the room.
// Or call switchRoom to switch rooms. Log out of the current room successfully within the SDK.
// Developers can handle the logic of actively logging out of the room callback here.
}
else if(reason == ZegoRoomStateChangedReasonLogoutFailed)
{
// Logout failed
// The developer actively calls logoutRoom and fails to log out of the room.
// Or call switchRoom to switch rooms. Logout of the current room fails internally in the SDK.
// The reason for the error may be that the logout room ID is wrong or does not exist.
}
}
After you log in to a room, you may be disconnected from the room due to a network signal issue or network-type switching issue. In this case, the SDK automatically initiates a reconnection. The following figure shows changes in the room connection state of a user A when the user A and a user B both have logged into the same room.
After a room disconnection occurs, the following three reconnection scenarios may be involved:
Scenario 1: Reconnection succeeds before the server determines that the heartbeat of the user A times out
Scenario 2: Reconnection succeeds after the server determines that the heartbeat of the user A times out
The definitions of T0, T1, T2, and T3 in this scenario are the same as those in the scenario 1.
T4' is about 90 s later than T3. At T4', the user B receives the onRoomUserUpdate callback notification used to notify that the user A is disconnected.
T5' specifies a point in time (greater than 90 s but less than 20 min) later than T3. At T5', the user A restores network connection and is reconnected to the room. The user A receives the onRoomStateChanged callback notification used to notify the client that the user A is reconnected to the room.
T6' is about 100 ms later than T5'. Due to a network transmission delay, the user B receives the onRoomUserUpdate callback notification about 100 ms later than T5'. The notification is used to notify the client that the user A joins the room.
Scenario 3: The user A fails to reconnect to the room
The definitions of T0, T1, T2, and T3 in this scenario are the same as those in the scenario 1.
T4'' is about 90 s later than T3. At T4'', the user B receives the onRoomUserUpdate callback notification used to notify that the user A is disconnected.
T5'' is 20 minutes later than T3. If the user A fails to join the room within 20 minutes after T3, the SDK does not attempt to reconnect to the room. The user A receives the onRoomStateChanged callback notification used to notify the client that the reconnection fails.
Does Express SDK support the disconnection and reconnection mechanism?