- Android
- iOS
- JavaScript
- Flutter
- ReactNative
- HarmonyOS
You can register multiple listeners.
JIM.getInstance().getConnectionManager().addConnectionStatusListener("main", new IConnectionManager.IConnectionStatusListener() {
/// connection-status change callback
/// - Parameters:
/// - status: updated status
/// - code: connection error code; valid in the JIMConst.ConnectionStatus.FAILURE state and 0 in all other states.
/// - extra: additional information
@Override
public void onStatusChange(JIMConst.ConnectionStatus status, int code, String extra) {
if (status == JIMConst.ConnectionStatus.CONNECTED) {
Log.i("TAG", "SDK connect success");
}
}
/// database-open callback; the local database can be opened to view message history while offline
@Override
public void onDbOpen() {
Log.i("TAG", "onDbOpen");
}
@Override
public void onDbClose() {
}
});
You can register multiple delegates.
[JIM.shared.connectionManager addDelegate:self];
/// database-open callback; the local database can be opened to view message history while offline
- (void)dbDidOpen {
NSLog(@"dbDidOpen");
}
/// connection-status change callback
/// - Parameters:
/// - status: updated status
/// - code: connection error code; valid in the JConnectionStatusFailure state and 0 in all other states.
/// - extra: additional information
- (void)connectionStatusDidChange:(JConnectionStatus)status
errorCode:(JErrorCode)code
extra:(NSString *)extra {
if (JConnectionStatusConnected == status) {
NSLog(@"SDK connect success");
}
}
Configure this listener only once globally; subsequent calls overwrite the previous listener. Except for connect, call all SDK methods only after connecting successfully. For Event details, see listener enums.
let { Event, ConnectionState } = JIM;
jim.on(Event.STATE_CHANGED, ({ state, user }) => {
if(ConnectionState.CONNECTING == state){
console.log('im is connecting');
}
if(ConnectionState.CONNECTED == state){
console.log('im is connected', user);
}
// The SDK handles reconnection internally. Application code does not need to reconnect. A failed reconnection or explicit disconnection triggers the DISCONNECTED state
if(ConnectionState.DISCONNECTED == state){
console.log('im is disconnected');
}
});
You can register multiple listeners.
JuggleIM\.instance.getConnectionManager().addConnectStatusListener((status,code)=>{
if(status === ConnStatus.Connected){
}
})
You can register multiple listeners. Each listener requires a unique key, and the returned function removes the listener.
Example
import JuggleIM from 'juggleim-rnsdk';
// Add a connection-status listener. Returns a function that removes the listener.
const unsubscribeConnection = JuggleIM.addConnectionStatusListener('connection_key', (status, code, extra) => {
// status: connection status: 'connected' | 'connecting' | 'disconnected' | 'failure'
// code: status code; contains an error code when connection fails
// extra: additional information
if (status === 'connected') {
// connected
}
if (status === 'connecting') {
// connecting
}
if (status === 'disconnected') {
// disconnected
}
if (status === 'failure') {
// connection failed; code contains the error code and extra contains the error message
}
});
// remove the listener
// unsubscribeConnection();
Only one connection listener is supported; subsequent configuration overwrites the previous listener. If multiple parts of the application need to listen, handle all states in one listener and distribute secondary events in your application.
Example
JuggleIM\.instance.onConnectionStatusChange = (int connectionStatus, int code, String extra){
if(connectionStatus == SDKConnectionStatus.CONNECTED){
// connected
}
if(connectionStatus == SDKConnectionStatus.CONNECTING){
// connecting
}
if(connectionStatus == SDKConnectionStatus.DISCONNECTED){
// disconnected
}
if(connectionStatus == SDKConnectionStatus.FAILURE){
// connection failed; code contains the error code and extra contains the error message
}
}
// database-open callback; the local database can be opened to view message history while offline
JuggleIM\.instance.onDbOpen = () {
};