Initialize the SDK in the Application’s onCreate method:
Copy
/** * Initialize IM * @param context Application Context * @param uid Login user ID (uid registered with IM communication end by business server) * @param token Login user token (token registered with IM communication end by business server) */WKIM.getInstance().init(context, uid, token);
Listen for events to get connection server IP and Port:
Copy
WKIM.getInstance().getConnectionManager().addOnGetIpAndPortListener(new IGetIpAndPort() { @Override public void getIP(IGetSocketIpAndPortListener iGetSocketIpAndPortListener) { // Return connection IP and port iGetSocketIpAndPortListener.onGetSocketIpAndPort("xxx.xxx.xxx.xxx", 5100); }});
Return the IP of the IM communication end and the TCP port of the IM communication end. For distributed systems, call the interface to get IP and Port before returning.
WKIM.getInstance().getConnectionManager().addOnConnectionStatusListener("key", new IConnectionStatus() { @Override public void onStatus(int status, String reason) { switch (status) { case WKConnectStatus.success: // Connection successful break; case WKConnectStatus.failed: // Connection failed break; case WKConnectStatus.connecting: // Connecting break; case WKConnectStatus.syncMsg: // Syncing messages break; case WKConnectStatus.noNetwork: // No network break; case WKConnectStatus.kicked: // Kicked offline - need to exit app and return to login page break; } }});
// Remove specific listenerWKIM.getInstance().getConnectionManager().removeOnConnectionStatusListener("key");// Remove all listenersWKIM.getInstance().getConnectionManager().removeAllOnConnectionStatusListener();