Connection Status
The IMLib SDK provides the ConnectionStatusListener
interface for monitoring IM connection status changes. By listening to these changes, you can implement different business logic or display user notifications.
Connection Status Listener Overview
The ConnectionStatusListener
interface is defined as follows:
public interface ConnectionStatusListener {
void onChanged(ConnectionStatus status);
}
When the connection status changes, the IMLib SDK notifies developers through the onChanged()
callback. Refer to the table below for status descriptions.
Status Name | Value | Description |
---|---|---|
NETWORK_UNAVAILABLE | -1 | Network unavailable |
CONNECTED | 0 | Connection established |
CONNECTING | 1 | Connecting |
UNCONNECTED | 2 | Initial state before initiating connection |
KICKED_OFFLINE_BY_OTHER_CLIENT | 3 | Current device logged out due to login on another device |
TOKEN_INCORRECT | 4 | Triggered when Token expires |
CONN_USER_BLOCKED | 6 | User banned via Console |
SIGN_OUT | 12 | Manual disconnection state (see Disconnect) |
SUSPEND | 13 | Temporary connection suspension (usually network-related). SDK will auto-reconnect. |
TIMEOUT | 14 | Connection timeout. SDK stops retrying. Manual reconnection via Connect API required. |
Setting Up the Listener
We recommend setting this up during the app lifecycle. Remove the listener when no longer needed to prevent memory leaks.
private RongIMClient.ConnectionStatusListener connectionStatusListener = new RongIMClient.ConnectionStatusListener() {
@Override
public void onChanged(ConnectionStatus status) {
// Implement business logic based on status codes
}
};
public void setIMStatusListener() {
RongIM.setConnectionStatusListener(connectionStatusListener);
}
Checking Current Connection Status
Use getCurrentConnectionStatus
to actively retrieve the current IM connection state.
tip
If the SDK initiates reconnection when checking status, the returned state might temporarily show as "connected".
Sample Code
RongCoreClient.getInstance().getCurrentConnectionStatus()