forked from dashscope/dashscope-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioWebsocketRequest.java
More file actions
153 lines (135 loc) · 4.42 KB
/
Copy pathAudioWebsocketRequest.java
File metadata and controls
153 lines (135 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 == true) {
log.debug("send message: " + message);
}
Boolean isOk = websocktetClient.send(message);
}
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();
websocktetClient.send(rawData);
}
@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();
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);
}
}
}