Monitor Connection Status
The client SDK provides an IM connection status listener, ConnectionStatusListener
, for apps. By monitoring changes in the IM connection status, apps can handle different business logic or display prompts on the interface.
Connection Status Listener Overview
The ConnectionStatusListener
interface is defined as follows:
public interface ConnectionStatusListener {
void onChanged(ConnectionStatus status);
}
When the connection status changes, the SDK will callback the current connection status to developers via 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 has not called the connect 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 the Console |
SIGN_OUT | 12 | State where the user actively disconnects, see Disconnect |
SUSPEND | 13 | Connection temporarily suspended (usually due to network issues), the SDK will automatically reconnect at an appropriate time |
TIMEOUT | 14 | Connection timeout, the SDK will stop connecting, and the user needs to handle the timeout and manually call the Connect Interface to reconnect |
Add or Remove Connection Status Listeners
The SDK supports setting multiple listeners. It is recommended to set them within the app's lifecycle.
To avoid memory leaks, remove the listeners when they are no longer needed.
// Add a connection status listener since 5.1.6
RongCoreClient.addConnectionStatusListener(listener);
// Remove a connection status listener since 5.1.6
RongCoreClient.removeConnectionStatusListener(listener);