Skip to content

Commit 4222d34

Browse files
committed
fix: socket send async
1 parent b7ac062 commit 4222d34

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spark-ai-browser",
3-
"version": "0.1.1",
3+
"version": "0.1.6",
44
"author": {
55
"name": "nianyi",
66
"email": "nianyi778@gmail.com",

src/index.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { generateUUID, getModelDomain, getUrl } from './utils';
22

33
interface SendBody {
44
uuid?: string; // 可以用来标记发送消息人身份,默认系统随机数
5-
content: string;
5+
content?: string;
66
payload?: {
77
message: {
88
text: {
@@ -68,7 +68,7 @@ export class Spark {
6868

6969
totalRes?: string;
7070

71-
ttsWS?: WebSocket;
71+
ttsWS?: WebSocket | null;
7272

7373
observers: Callback[];
7474

@@ -98,23 +98,29 @@ export class Spark {
9898
return this.status;
9999
}
100100

101-
start() {
101+
async start() {
102102
this.totalRes = ''; // 请空回答历史
103-
this.connectWebSocket();
103+
await this.connectWebSocket();
104104
}
105105

106106
// 连接websocket
107107
connectWebSocket() {
108-
this.setStatus(Status.ttsing);
109-
const { url } = this.option;
110-
if (url) {
108+
return new Promise<void>(resolve => {
109+
this.setStatus(Status.ttsing);
110+
const { url } = this.option;
111+
if (!url) {
112+
resolve();
113+
return;
114+
}
111115
if ('WebSocket' in window) {
112116
this.ttsWS = new WebSocket(url);
113117
} else {
118+
resolve();
114119
throw new Error('浏览器不支持WebSocket');
115120
return;
116121
}
117122
this.ttsWS.onopen = () => {
123+
resolve();
118124
// this.webSocketSend();
119125
console.info('WebSocket connection');
120126
};
@@ -123,15 +129,17 @@ export class Spark {
123129
this.messageDataChange(e.data);
124130
};
125131
this.ttsWS.onerror = () => {
132+
this.ttsWS = null;
126133
this.setStatus(Status.error);
127134
console.info('WebSocket报错,请f12查看详情');
128135
console.error(`详情查看:${encodeURI(url.replace('wss:', 'https:'))}`);
129136
};
130137
this.ttsWS.onclose = e => {
131-
console.log(e);
138+
console.error(e);
132139
this.setStatus(Status.close);
140+
this.ttsWS = null;
133141
};
134-
}
142+
});
135143
}
136144

137145
// 监听变化
@@ -178,12 +186,17 @@ export class Spark {
178186
}
179187

180188
// websocket发送数据
181-
webSocketSend({
189+
async webSocketSend({
182190
uuid = generateUUID(),
183191
content,
184192
payload,
185193
parameter,
186194
}: SendBody) {
195+
await this.start();
196+
if (!this.ttsWS) {
197+
console.error('socket is not create');
198+
return;
199+
}
187200
const params = {
188201
header: {
189202
app_id: this.option.appId,

src/utils/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ export function getUrl(version: OptionType['version']) {
4747

4848
type UUID = string;
4949

50-
export function generateUUID(): UUID {
50+
export function generateUUID(idStyle = 'xxxxx'): UUID {
5151
let d = new Date().getTime();
52-
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[x]/g, () => {
52+
53+
const uuid = idStyle.replace(/[x]/g, () => {
5354
const r = (d + Math.random() * 16) % 16;
5455
d = Math.floor(d / 16);
5556
return r.toString(16);

0 commit comments

Comments
 (0)