Initialization
Initialize the SDK in the Application’s onCreate method:
/**
* 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);
Advanced Initialization
You can also initialize with custom options:
WKIMOptions options = new WKIMOptions();
options.setLogLevel(WKLogLevel.DEBUG);
options.setDbPassword("your_db_password");
options.setFileUploadUrl("https://your-upload-server.com/upload");
WKIM.getInstance().init(context, uid, token, options);
Server Configuration
Listen for events to get connection server IP and Port:
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.
Connection Management
Connect
// Connect to IM
WKIM.getInstance().getConnectionManager().connection();
Disconnect
// Disconnect IM
WKIM.getInstance().getConnectionManager().disconnect(isLogout);
Parameters:
isLogout:
true: SDK will no longer reconnect
false: SDK maintains reconnection mechanism
Connection Status Monitoring
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 Connection Status Listener
// Remove specific listener
WKIM.getInstance().getConnectionManager().removeOnConnectionStatusListener("key");
// Remove all listeners
WKIM.getInstance().getConnectionManager().removeAllOnConnectionStatusListener();
Connection Status Types
| Status | Description |
|---|
WKConnectStatus.success | Connection successful |
WKConnectStatus.failed | Connection failed |
WKConnectStatus.connecting | Connecting to server |
WKConnectStatus.syncMsg | Syncing messages |
WKConnectStatus.noNetwork | No network available |
WKConnectStatus.kicked | Kicked offline by another device |
Best Practices
1. Application Lifecycle Management
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize SDK
WKIM.getInstance().init(this, "user123", "user-token");
// Setup connection listener
setupConnectionListener();
// Setup server configuration
setupServerConfig();
}
private void setupConnectionListener() {
WKIM.getInstance().getConnectionManager().addOnConnectionStatusListener("app",
(status, reason) -> {
Log.d("WuKongIM", "Connection status: " + status + ", reason: " + reason);
if (status == WKConnectStatus.kicked) {
// Handle being kicked offline
handleKickedOffline();
}
});
}
private void setupServerConfig() {
WKIM.getInstance().getConnectionManager().addOnGetIpAndPortListener(listener -> {
// In production, get from your server
listener.onGetSocketIpAndPort("your-server.com", 5100);
});
}
private void handleKickedOffline() {
// Redirect to login screen
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
2. Activity Lifecycle Integration
public class MainActivity extends AppCompatActivity {
@Override
protected void onResume() {
super.onResume();
// Connect when app comes to foreground
if (!WKIM.getInstance().getConnectionManager().isConnected()) {
WKIM.getInstance().getConnectionManager().connection();
}
}
@Override
protected void onPause() {
super.onPause();
// Optionally disconnect when app goes to background
// WKIM.getInstance().getConnectionManager().disconnect(false);
}
}
3. Network State Monitoring
public class NetworkMonitor extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
// Network available, try to connect
WKIM.getInstance().getConnectionManager().connection();
} else {
// Network unavailable
Log.d("WuKongIM", "Network unavailable");
}
}
}
Troubleshooting
Common Issues
-
Connection Failed
- Check if server IP and port are correct
- Verify network connectivity
- Ensure user credentials are valid
-
Frequent Disconnections
- Check network stability
- Verify server availability
- Review connection timeout settings
-
Kicked Offline
- Handle gracefully by redirecting to login
- Clear user session data
- Notify user about the logout
Next Steps