Monitor Connection Status
Feature Description
Use addEventListener to monitor the IM connection status. Based on the connection status, you can handle different business logic or provide prompts on the page.
- It is recommended to set this up during the application lifecycle.
- When the application is destroyed, these event listeners should be cleaned up.
Example Usage
const Events = RongIMLib.Events
/**
* Event status when connecting
*/
RongIMLib.addEventListener(Events.CONNECTING, () => {
console.log('Connecting...')
})
/**
* This event is triggered when connected to the server
*/
RongIMLib.addEventListener(Events.CONNECTED, () => {
console.log('Connected')
})
/**
* This event is triggered when manually calling the disconnect method or when the user is kicked offline
*/
RongIMLib.addEventListener(Events.DISCONNECT, (code) => {
console.log('Connection interrupted, business layer needs to handle reconnection ->', code)
})
/**
* This event is triggered when there is a connection issue and the SDK attempts to reconnect internally
*/
RongIMLib.addEventListener(Events.SUSPEND, (code) => {
console.log('Connection interrupted, the SDK will attempt to reconnect, no need for business layer concern')
// Starting from version 5.1.2, the code status that may cause interruption is included in the event callback
console.log(`code -> ${code}`)
})
tip
- Before version 5.6.1, the
code
parameter in the Events.DISCONNECT and Events.SUSPEND callback functions is of type ConnectionStatus - After version 5.7.0, the
code
parameter in the Events.DISCONNECT and Events.SUSPEND callback functions is of type ErrorCode
Connection Event Descriptions
Event | Description |
---|---|
Events.CONNECTING | IM is connecting |
Events.CONNECTED | IM connection is complete |
Events.DISCONNECT | IM connection is interrupted, the business layer needs to handle reconnection |
Events.SUSPEND | IM connection is interrupted, the SDK is responsible for attempting to reconnect |