在进行 socket 连接前先关闭之前连接。实现新建会话功能。

This commit is contained in:
RockYang
2023-04-19 15:18:13 +08:00
parent 50ff591dbb
commit 14a351b477
8 changed files with 158 additions and 22 deletions

View File

@@ -238,6 +238,7 @@ export default defineComponent({
connectingMessageBox: null, // 保存重连的消息框对象
errorMessage: null, // 错误信息提示框
socket: null,
activelyClose: false, // 主动关闭
mainWinHeight: 0, // 主窗口高度
chatBoxHeight: 0, // 聊天内容框高度
leftBoxHeight: 0,
@@ -284,6 +285,12 @@ export default defineComponent({
},
// 创建 socket 会话连接
connect: function () {
// 先关闭已有连接
if (this.socket !== null) {
this.activelyClose = true;
this.socket.close();
}
// 初始化 WebSocket 对象
const sessionId = getSessionId();
const socket = new WebSocket(process.env.VUE_APP_WS_HOST + `/api/chat?sessionId=${sessionId}&role=${this.role}`);
@@ -303,6 +310,7 @@ export default defineComponent({
}
this.sending = false; // 允许用户发送消息
this.activelyClose = false;
if (this.errorMessage !== null) {
this.errorMessage.close(); // 关闭错误提示信息
}
@@ -359,6 +367,10 @@ export default defineComponent({
});
socket.addEventListener('close', () => {
if (this.activelyClose) { // 忽略主动关闭
return;
}
// 停止送消息
this.sending = true;
this.checkSession();