Monitoring Connection Status
The IMLib SDK provides the RCConnectionStatusChangeDelegate protocol for apps to monitor IM connection status changes. By listening to these changes, apps can perform different business processes or display prompts on the interface.
Adding or Removing Delegate Listeners
You can set connection status listeners after initializing the AppKey in IMLib SDK but before connecting to IM. Multiple listeners are supported.
To prevent memory leaks, remove the delegate when monitoring is no longer needed.
Assign the delegate object to a singleton like AppDelegate to ensure the protocol methods can be monitored throughout the entire app lifecycle.
/// Add delegate listener
[[RCCoreClient sharedCoreClient] addConnectionStatusChangeDelegate:self];
/// Remove delegate listener
[[RCCoreClient sharedCoreClient] removeConnectionStatusChangeDelegate:self];
Connection Status Change Delegate Protocol
The RCConnectionStatusChangeDelegate protocol is defined as follows:
@protocol RCConnectionStatusChangeDelegate <NSObject>
/*!
IMLib connection status listener
@param status Connection status between SDK and RC server
@discussion After setting up the IMLib connection listener, this method will be called back when the connection status between SDK and RC server changes.
*/
- (void)onConnectionStatusChanged:(RCConnectionStatus)status;
@end
When the connection status changes, the SDK will callback the current status to developers through - (void)onConnectionStatusChanged:(RCConnectionStatus)status;. RCConnectionStatus defines possible connection states during the process. The table below details status codes that require app handling.
| Error Code | Value | Description |
|---|---|---|
| ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT | 6 | Current user logged in on another device, this device was kicked offline |
| ConnectionStatus_Timeout | 14 | Auto-connection timeout. SDK will stop reconnecting. User needs to handle timeout and manually call connectWithToken to reconnect |
| ConnectionStatus_TOKEN_INCORRECT | 15 | Invalid token. Retrieve a new token from app server. Two possible reasons: 1. Token error - verify AppKey used for client initialization matches the one used for token retrieval on server 2. Token expired - token expiration time was set in Console. Request a new token from developer server and reconnect with it. |
| ConnectionStatus_DISCONN_EXCEPTION | 16 | Disconnected from server due to user ban |
Retrieving Current Connection Status
You can actively obtain the current IM service connection status using the getConnectionStatus method.