Skip to main content
Responsible for establishing, maintaining, and disconnecting IM connections. This documentation only covers core methods. For more details, check the [WKSDK shared].connectionManager interface in the code.

Configuration

[WKSDK shared].options.host = @"xxx.xxx.xxx.xxx"; // IM communication IP
[WKSDK shared].options.port = 5100; // IM communication TCP port

// Set IM connection authentication information
[WKSDK shared].options.connectInfoCallback = ^WKConnectInfo * _Nonnull{
    WKConnectInfo *connectInfo = [WKConnectInfo new];
    connectInfo.uid = "xxxx"; // User uid (registered with IM communication end by business server)
    connectInfo.token = "xxxx"; // User token (registered with IM communication end by business server)
    return  connectInfo;
};
For more configuration options, check [WKSDK shared].options

Swift Configuration

WKSDK.shared.options.host = "xxx.xxx.xxx.xxx" // IM communication IP
WKSDK.shared.options.port = 5100 // IM communication TCP port

// Set IM connection authentication information
WKSDK.shared.options.connectInfoCallback = {
    let connectInfo = WKConnectInfo()
    connectInfo.uid = "xxxx" // User uid
    connectInfo.token = "xxxx" // User token
    return connectInfo
}

Advanced Configuration

// Set connection timeout
[WKSDK shared].options.connectTimeout = 10; // 10 seconds

// Set heartbeat interval
[WKSDK shared].options.heartbeatInterval = 30; // 30 seconds

// Set auto-reconnect
[WKSDK shared].options.autoReconnect = YES;

// Set max reconnect attempts
[WKSDK shared].options.maxReconnectAttempts = 10;

// Set reconnect interval
[WKSDK shared].options.reconnectInterval = 5; // 5 seconds

Connect

// Connect
[[WKSDK shared].connectionManager connect];
// Swift
WKSDK.shared.connectionManager.connect()

Disconnect

// Disconnect - NO: SDK maintains reconnection mechanism, YES: SDK will no longer reconnect
[[WKSDK shared].connectionManager disconnect:NO];
// Swift
WKSDK.shared.connectionManager.disconnect(false) // false: maintain reconnection

Connection Status Monitoring

[WKSDK.shared.connectionManager addDelegate:self]; // WKConnectionManagerDelegate
// Swift
WKSDK.shared.connectionManager.addDelegate(self) // WKConnectionManagerDelegate

Delegate Implementation

// ---------- WKConnectionManagerDelegate ----------

/**
 Connection status monitoring
 */
-(void) onConnectStatus:(WKConnectStatus)status reasonCode:(WKReason)reasonCode {
    switch (status) {
        case WKConnecting:
            NSLog(@"Connecting...");
            break;
        case WKConnected:
            NSLog(@"Connected successfully!");
            break;
        case WKDisconnected:
            NSLog(@"Disconnected, reason: %d", reasonCode);
            break;
        case WKConnectFail:
            NSLog(@"Connection failed, reason: %d", reasonCode);
            break;
    }
}

/**
 Connection kicked off (logged in from another device)
 */
-(void) onKick:(WKReason)reasonCode {
    NSLog(@"Kicked off, reason: %d", reasonCode);
    // Handle being kicked off, usually show login page
}
// Swift implementation
extension YourViewController: WKConnectionManagerDelegate {
    func onConnectStatus(_ status: WKConnectStatus, reasonCode: WKReason) {
        switch status {
        case .connecting:
            print("Connecting...")
        case .connected:
            print("Connected successfully!")
        case .disconnected:
            print("Disconnected, reason: \(reasonCode)")
        case .connectFail:
            print("Connection failed, reason: \(reasonCode)")
        @unknown default:
            break
        }
    }
    
    func onKick(_ reasonCode: WKReason) {
        print("Kicked off, reason: \(reasonCode)")
        // Handle being kicked off
    }
}

Connection Status Types

StatusDescription
WKConnectingConnecting to server
WKConnectedSuccessfully connected
WKDisconnectedDisconnected from server
WKConnectFailConnection failed

Reason Codes

Common reason codes for connection status changes:
CodeDescription
WKReasonConnectSuccessConnection successful
WKReasonConnectTimeoutConnection timeout
WKReasonAuthFailAuthentication failed
WKReasonNetworkErrorNetwork error
WKReasonKickOffKicked off by another device

Best Practices

1. Connection Lifecycle Management

// In AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // App becomes active, connect if needed
    if (![WKSDK.shared.connectionManager isConnected]) {
        [WKSDK.shared.connectionManager connect];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // App enters background, you may choose to disconnect
    // [WKSDK.shared.connectionManager disconnect:NO];
}

2. Network Status Monitoring

#import <SystemConfiguration/SystemConfiguration.h>

// Monitor network reachability
- (void)startNetworkMonitoring {
    // Implementation depends on your network monitoring solution
    // When network becomes available, reconnect
    [WKSDK.shared.connectionManager connect];
}

3. Error Handling

-(void) onConnectStatus:(WKConnectStatus)status reasonCode:(WKReason)reasonCode {
    if (status == WKConnectFail) {
        switch (reasonCode) {
            case WKReasonAuthFail:
                // Handle authentication failure - refresh token
                [self refreshTokenAndReconnect];
                break;
            case WKReasonNetworkError:
                // Handle network error - retry later
                [self scheduleReconnect];
                break;
            default:
                break;
        }
    }
}

Next Steps