-
Notifications
You must be signed in to change notification settings - Fork 22
(feat:model/cosyvoice):use new protocol to build websocket connection #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/com/alibaba/dashscope/audio/protocol/AudioWebsocketCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.alibaba.dashscope.audio.protocol; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import okhttp3.WebSocket; | ||
|
|
||
| /** @author songsong.shao */ | ||
| public interface AudioWebsocketCallback { | ||
|
|
||
| void onOpen(); | ||
|
|
||
| void onMessage(WebSocket webSocket, String text); | ||
|
|
||
| void onMessage(WebSocket webSocket, ByteBuffer buffer); | ||
|
|
||
| void onError(WebSocket webSocket, Throwable t); | ||
|
|
||
| void onClose(int code, String reason); | ||
| } |
157 changes: 157 additions & 0 deletions
157
src/main/java/com/alibaba/dashscope/audio/protocol/AudioWebsocketRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,157 @@ | ||||||||||
| package com.alibaba.dashscope.audio.protocol; | ||||||||||
|
|
||||||||||
| import com.alibaba.dashscope.exception.NoApiKeyException; | ||||||||||
| import com.alibaba.dashscope.protocol.DashScopeHeaders; | ||||||||||
| import com.alibaba.dashscope.protocol.okhttp.OkHttpClientFactory; | ||||||||||
| import com.alibaba.dashscope.utils.ApiKey; | ||||||||||
| import com.alibaba.dashscope.utils.Constants; | ||||||||||
| import java.util.Map; | ||||||||||
| import java.util.concurrent.CountDownLatch; | ||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||
| import java.util.concurrent.atomic.AtomicBoolean; | ||||||||||
| import java.util.concurrent.atomic.AtomicReference; | ||||||||||
| import lombok.extern.slf4j.Slf4j; | ||||||||||
| import okhttp3.*; | ||||||||||
| import okio.ByteString; | ||||||||||
|
|
||||||||||
| /** @author songsong.shao */ | ||||||||||
| @Slf4j | ||||||||||
| public class AudioWebsocketRequest extends WebSocketListener { | ||||||||||
|
|
||||||||||
| private OkHttpClient client; | ||||||||||
| private WebSocket websocktetClient; | ||||||||||
| private AtomicBoolean isOpen = new AtomicBoolean(false); | ||||||||||
| private AtomicReference<CountDownLatch> connectLatch = new AtomicReference<>(null); | ||||||||||
| private AtomicBoolean isClosed = new AtomicBoolean(false); | ||||||||||
| private AudioWebsocketCallback callback; | ||||||||||
| private Integer connectTimeout = 5000; | ||||||||||
|
|
||||||||||
| public boolean isOpen() { | ||||||||||
| return isOpen.get(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public boolean isClosed() { | ||||||||||
| return isClosed.get(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public void checkStatus() { | ||||||||||
| if (this.isClosed.get()) { | ||||||||||
| throw new RuntimeException("Websocket is already closed!"); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public void connect( | ||||||||||
| String apiKey, | ||||||||||
| String workspace, | ||||||||||
| Map<String, String> customHeaders, | ||||||||||
| String baseWebSocketUrl, | ||||||||||
| AudioWebsocketCallback callback) | ||||||||||
| throws NoApiKeyException, InterruptedException, RuntimeException { | ||||||||||
| Request request = | ||||||||||
| buildConnectionRequest( | ||||||||||
| ApiKey.getApiKey(apiKey), false, workspace, customHeaders, baseWebSocketUrl); | ||||||||||
| this.callback = callback; | ||||||||||
| client = OkHttpClientFactory.getOkHttpClient(); | ||||||||||
| websocktetClient = client.newWebSocket(request, this); | ||||||||||
| connectLatch.set(new CountDownLatch(1)); | ||||||||||
| boolean result = connectLatch.get().await(connectTimeout, TimeUnit.MILLISECONDS); | ||||||||||
| if (!result) { | ||||||||||
| throw new RuntimeException( | ||||||||||
| "TimeoutError: waiting for websocket connect more than" + connectTimeout + " ms."); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private Request buildConnectionRequest( | ||||||||||
| String apiKey, | ||||||||||
| boolean isSecurityCheck, | ||||||||||
| String workspace, | ||||||||||
| Map<String, String> customHeaders, | ||||||||||
| String baseWebSocketUrl) | ||||||||||
| throws NoApiKeyException { | ||||||||||
| // build the request builder. | ||||||||||
| Request.Builder bd = new Request.Builder(); | ||||||||||
| bd.headers( | ||||||||||
| Headers.of( | ||||||||||
| DashScopeHeaders.buildWebSocketHeaders( | ||||||||||
| apiKey, isSecurityCheck, workspace, customHeaders))); | ||||||||||
| String url = Constants.baseWebsocketApiUrl; | ||||||||||
| if (baseWebSocketUrl != null) { | ||||||||||
| url = baseWebSocketUrl; | ||||||||||
| } | ||||||||||
| Request request = bd.url(url).build(); | ||||||||||
| return request; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private void sendMessage(String message, boolean enableLog) { | ||||||||||
| checkStatus(); | ||||||||||
| if (enableLog) { | ||||||||||
| log.debug("send message: " + message); | ||||||||||
| } | ||||||||||
| if (!websocktetClient.send(message)) { | ||||||||||
| log.warn("Failed to enqueue websocket text message for sending."); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public void close() { | ||||||||||
| this.close(1000, "bye"); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public void close(int code, String reason) { | ||||||||||
| checkStatus(); | ||||||||||
| websocktetClient.close(code, reason); | ||||||||||
| isClosed.set(true); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public void sendTextMessage(String message) { | ||||||||||
| checkStatus(); | ||||||||||
| this.sendMessage(message, true); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public void sendBinaryMessage(ByteString rawData) { | ||||||||||
| checkStatus(); | ||||||||||
| if (!websocktetClient.send(rawData)) { | ||||||||||
| log.warn("Failed to enqueue websocket binary message for sending."); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void onOpen(WebSocket webSocket, Response response) { | ||||||||||
| isOpen.set(true); | ||||||||||
| connectLatch.get().countDown(); | ||||||||||
| log.debug("WebSocket opened"); | ||||||||||
| callback.onOpen(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void onMessage(WebSocket webSocket, String text) { | ||||||||||
| callback.onMessage(webSocket, text); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void onMessage(WebSocket webSocket, ByteString bytes) { | ||||||||||
| log.debug("Received binary message"); | ||||||||||
| callback.onMessage(webSocket, bytes.asByteBuffer()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void onClosed(WebSocket webSocket, int code, String reason) { | ||||||||||
| isOpen.set(false); | ||||||||||
| isClosed.set(true); | ||||||||||
| connectLatch.get().countDown(); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call to
Suggested change
|
||||||||||
| log.debug("WebSocket closed"); | ||||||||||
| callback.onClose(code, reason); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void onFailure(WebSocket webSocket, Throwable t, Response response) { | ||||||||||
| log.error("WebSocket failed: " + t.getMessage()); | ||||||||||
| if (connectLatch.get() != null) { | ||||||||||
| connectLatch.get().countDown(); | ||||||||||
| } | ||||||||||
| if (callback != null) { | ||||||||||
| callback.onError(webSocket, t); | ||||||||||
| } else { | ||||||||||
| throw new RuntimeException(t); | ||||||||||
| } | ||||||||||
| } | ||||||||||
songguocola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| } | ||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.