Skip to content

Commit

Permalink
fix: promise has not resolve #21
Browse files Browse the repository at this point in the history
  • Loading branch information
OrenZhang committed Jun 7, 2024
1 parent a7f3f42 commit 9911ba5
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/components/ChatInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,10 @@ const onMessage = (event) => {
const sendMessage = (message) => {
initWebSocket().then(
() => {
if (webSocket.value && webSocket.value.readyState === WebSocket.OPEN) {
webSocket.value.send(message);
} else {
Message.error(i18n.t('ConnectionClosedPleaseRetry'));
}
webSocket.value.send(message);
},
() => {
Message.error(i18n.t('ConnectionClosedPleaseRetry'));
},
);
};
Expand All @@ -138,9 +137,20 @@ const sendMessage = (message) => {
const webSocket = ref(null);
const retryTimes = ref(0);
const maxRetryTimes = ref(1000);
const buildWebSocketPromise = () => {
return new Promise((resolve, reject) => {
webSocket.value.onopen = (e) => {
resolve(e);
};
webSocket.value.onerror = (e) => {
emits('setChatLoading', false);
reject(e);
};
});
};
const initWebSocket = () => {
if (webSocket.value && webSocket.value.readyState === WebSocket.OPEN) {
return new Promise();
return buildWebSocketPromise();
}
webSocket.value = new WebSocket(`${globalContext.webSocketUrl}/chat/`);
webSocket.value.onmessage = (e) => {
Expand All @@ -149,12 +159,7 @@ const initWebSocket = () => {
webSocket.value.onclose = () => {
emits('setChatLoading', false);
};
webSocket.value.onerror = (e) => {
emits('setChatLoading', false);
};
return new Promise((resolve) => {
webSocket.value.onopen = (e) => resolve(e);
});
return buildWebSocketPromise();
};
const closeWebSocket = () => {
if (webSocket.value && webSocket.value.readyState === WebSocket.OPEN) {
Expand Down

0 comments on commit 9911ba5

Please sign in to comment.