@@ -71,6 +71,9 @@ dependencies {
7171| how to handle exception | [ HandlerExceptionExample.java] ( example/src/main/java/example/service/HandlerExceptionExample.java ) |
7272| get request log id | [ GetLogExample.java] ( example/src/main/java/example/service/GetLogExample.java ) |
7373| set timeout | [ SetRequestTimeoutExample.java] ( example/src/main/java/example/service/SetRequestTimeoutExample.java ) |
74+ | websocket chat | [ ChatExample.java] ( example/src/main/java/example/websocket/chat/ChatExample.java ) |
75+ | websocket speech synthesis | [ WebsocketAudioSpeechExample.java] ( example/src/main/java/example/websocket/audio/speech/WebsocketAudioSpeechExample.java ) |
76+ | websocket transcription | [ WebsocketTranscriptionsExample.java] ( example/src/main/java/example/websocket/audio/transcriptions/WebsocketTranscriptionsExample.java ) |
7477
7578### Initialize the Coze Client
7679
@@ -984,4 +987,123 @@ while (iterator.hasNext()) {
984987
985988```
986989
990+ ### WebSocket
991+
992+ The SDK provides WebSocket interfaces for real-time chat, speech synthesis and speech transcription.
993+
994+ You can check the official documentation for more information:
995+ https://www.coze.cn/open/docs/guides/websocket_openapi
996+
997+ #### WebSocket Chat
998+
999+ WebSocket chat allows real-time communication with bots, including text and audio interactions:
1000+
1001+ ``` java
1002+ WebsocketChatClient client = coze. websocket()
1003+ .chat()
1004+ .create(new WebsocketChatCreateReq (botID, new CallbackHandler ()));
1005+
1006+ // Send audio data
1007+ String audioData = " ..." ; // Base64 encoded audio data
1008+ client. inputAudioBufferAppend(audioData);
1009+ client. inputAudioBufferComplete();
1010+
1011+ // Handle responses in callback
1012+ class CallbackHandler extends WebsocketChatCallbackHandler {
1013+ // Handle text responses
1014+ @Override
1015+ public void onConversationMessageDelta (WebsocketChatClient client , ConversationMessageDeltaEvent event ) {
1016+ System . out. printf(" Received: %s\n " , event. getData(). getContent());
1017+ }
1018+
1019+ // Handle audio responses
1020+ @Override
1021+ public void onConversationAudioDelta (WebsocketChatClient client , ConversationAudioDeltaEvent event ) {
1022+ byte [] audioData = event. getData(). getAudio();
1023+ // Process audio data...
1024+ }
1025+ }
1026+ ```
1027+
1028+ #### Speech Synthesis
1029+
1030+ WebSocket speech synthesis allows real-time text-to-speech conversion:
1031+
1032+ ``` java
1033+ WebsocketAudioSpeechClient client = coze. websocket()
1034+ .audio()
1035+ .speech()
1036+ .create(new WebsocketAudioSpeechCreateReq (new CallbackHandler ()));
1037+
1038+ // Configure audio output
1039+ OutputAudio outputAudio = OutputAudio . builder()
1040+ .voiceId(voiceID)
1041+ .codec(" pcm" )
1042+ .speechRate(50 )
1043+ .pcmConfig(PCMConfig . builder(). sampleRate(24000 ). build())
1044+ .build();
1045+ client. speechUpdate(new SpeechUpdateEventData (outputAudio));
1046+
1047+ // Send text for synthesis
1048+ client. inputTextBufferAppend(" Hello world!" );
1049+ client. inputTextBufferComplete();
1050+
1051+ // Handle synthesized audio in callback
1052+ class CallbackHandler extends WebsocketAudioSpeechCallbackHandler {
1053+ @Override
1054+ public void onSpeechAudioUpdate (WebsocketAudioSpeechClient client , SpeechAudioUpdateEvent event ) {
1055+ byte [] audioData = event. getDelta();
1056+ // Process audio data...
1057+ }
1058+ }
1059+ ```
1060+
1061+ #### Speech Transcription
1062+
1063+ WebSocket speech transcription provides real-time speech-to-text conversion:
1064+
1065+ ``` java
1066+ WebsocketAudioTranscriptionsClient client = coze. websocket()
1067+ .audio()
1068+ .transcriptions()
1069+ .create(new WebsocketAudioTranscriptionsCreateReq (new CallbackHandler ()));
1070+
1071+ // Configure audio input
1072+ InputAudio inputAudio = InputAudio . builder()
1073+ .sampleRate(24000 )
1074+ .codec(" pcm" )
1075+ .format(" wav" )
1076+ .channel(2 )
1077+ .build();
1078+ client. transcriptionsUpdate(new TranscriptionsUpdateEventData (inputAudio));
1079+
1080+ // Send audio for transcription
1081+ String audioData = " ..." ; // Base64 encoded audio data
1082+ client. inputAudioBufferAppend(audioData);
1083+ client. inputAudioBufferComplete();
1084+
1085+ // Handle transcription results in callback
1086+ class CallbackHandler extends WebsocketAudioTranscriptionsCallbackHandler {
1087+ @Override
1088+ public void onTranscriptionsMessageUpdate (
1089+ WebsocketAudioTranscriptionsClient client ,
1090+ TranscriptionsMessageUpdateEvent event ) {
1091+ System . out. println(event. getData(). getContent());
1092+ }
1093+ }
1094+ ```
1095+
1096+ All WebSocket clients support proper resource cleanup:
1097+
1098+ ``` java
1099+ try {
1100+ // Use the client...
1101+ } finally {
1102+ if (client != null ) {
1103+ client. close();
1104+ }
1105+ coze. shutdownExecutor();
1106+ }
1107+ ```
1108+
9871109
0 commit comments