Connection Status
The IMLib SDK provides the IM connection status listener ConnectionStatusListener for apps. By monitoring changes in IM connection status, you can implement different business logic or display prompts on the interface.
Connection Status Listener Description
The ConnectionStatusListener interface is defined as follows:
public interface ConnectionStatusListener {
void onChanged(ConnectionStatus status);
}
When the connection status changes, the IMLib SDK will callback the current connection status to you through the onChanged() method. Refer to the table below for detailed descriptions of each status.
| Status Name | Status Value | Description |
|---|---|---|
NETWORK_UNAVAILABLE | -1 | Network unavailable. |
CONNECTED | 0 | Connection successful. |
CONNECTING | 1 | Connecting. |
UNCONNECTED | 2 | Disconnected state, meaning the app hasn't called the connection method. |
KICKED_OFFLINE_BY_OTHER_CLIENT | 3 | The user account logged in on another device, causing this device to be kicked offline. |
TOKEN_INCORRECT | 4 | Triggered when the Token expires. |
CONN_USER_BLOCKED | 6 | User banned via Console. |
SIGN_OUT | 12 | User-initiated disconnection status. See Disconnect for details. |
SUSPEND | 13 | Connection temporarily suspended (usually due to network issues). The SDK will automatically reconnect when appropriate. |
TIMEOUT | 14 | Connection timeout. The SDK will stop connecting. Users need to handle the timeout and manually call the Connect API to reconnect. |
Adding or Removing Connection Status Listeners
The SDK supports setting multiple listeners. It's recommended to set them during the app's lifecycle.
To prevent memory leaks, remove the listeners when they're no longer needed.
Sample Code
// Add connection status listener since 5.1.6
RongCoreClient.addConnectionStatusListener(listener);
// Remove connection status listener since 5.1.6
RongCoreClient.removeConnectionStatusListener(listener);
Getting Current Connection Status
You can actively retrieve the current IM service connection status using the getCurrentConnectionStatus method.
When retrieving the connection status, if the SDK happens to start reconnecting, the returned status might still show as connected.
Sample Code
RongCoreClient.getInstance().getCurrentConnectionStatus()