Package 

Class AIChainClient


  • 
    public final class AIChainClient
    
                        

    Main client for IIP Dispatch Protocol.

    This class provides WebSocket-based real-time communication with the IIP Dispatch service. It supports STT (Speech-to-Text), NLU (Natural Language Understanding), and TTS (Text-to-Speech).

    Example usage:

    val client = AIChainClient.Builder()
        .appId("your-app-id")
        .appKey("your-app-key")
        .host("your-host")
        .sn("your-sn")
        .build()
    
    client.on("session.created") { args ->
        val sid = args[0] as String
        println("Session created: $sid")
    }
    
    client.connect(object : ConnectionCallback {
        override fun onSuccess() {
            println("Connected")
        }
        override fun onError(error: AIChainError) {
            println("Error: ${error.message}")
        }
    })
    • Method Detail

      • connect

         final Unit connect(ConnectionCallback callback)

        Establish WebSocket connection and wait for session creation.

        Parameters:
        callback - Callback for connection result
      • reconnect

         final Unit reconnect(ConnectionCallback callback)

        Manually reconnect to the server. Waits 200ms after disconnect to allow server cleanup.

        Parameters:
        callback - Callback for reconnection result
      • onAppBackground

         final Unit onAppBackground()

        通知 SDK App 进入后台。

        App 应在 Activity/Fragment 的 onStop() 或 ProcessLifecycleOwner 回调中调用此方法。 SDK 会记录前后台状态,在断开诊断日志中标注 "App状态: 后台",方便排查后台断连问题。

        注意:进入后台后 Android 系统可能施加 Doze/AppStandby 限制,导致心跳超时断连。 如不希望后台断连,建议在进入后台时主动调用 disconnect,回到前台时调用 connect。

      • onAppForeground

         final Unit onAppForeground()

        通知 SDK App 回到前台。

        App 应在 Activity/Fragment 的 onStart() 或 ProcessLifecycleOwner 回调中调用此方法。

      • beginSend

         final Unit beginSend()

        Generate a new CID for the next conversation.

      • cancel

         final Unit cancel()

        Cancel the current conversation. Sends response.cancel for all active response CIDs and clears their buffers.

      • cancelCid

         final Unit cancelCid(String cid)

        Cancel a specific conversation by cid. Sends response.cancel for the specified cid regardless of whether it's in activeResponseCids. Also removes the cid from activeResponseCids and rotates currentInputCid if it matches.

        Parameters:
        cid - The conversation ID to cancel
      • sendImage

         final String sendImage(String imageData, Map<String, Object> extend)

        Send an image.

        Parameters:
        imageData - Base64 encoded image data or URL
        extend - Optional extended data
      • on

         final Unit on(String event, EventListener listener)

        Add an event listener.

        Parameters:
        event - Event name
        listener - Event listener
      • off

         final Unit off(String event, EventListener listener)

        Remove an event listener.

        Parameters:
        event - Event name
        listener - Event listener to remove, or null to remove all listeners for this event