优化聊天会话管理,支持 websocket 断开重连之后能继续连接会话上下文

This commit is contained in:
RockYang
2023-03-24 14:24:49 +08:00
parent 2067aa3f83
commit bb019f3552
9 changed files with 244 additions and 317 deletions

View File

@@ -1,2 +1,2 @@
VUE_APP_API_HOST=http://chat.r9it.com:6789
VUE_APP_WS_HOST=ws://chat.r9it.com:6789
VUE_APP_API_HOST=https://ai.r9it.com
VUE_APP_WS_HOST=wss://ai.r9it.com

341
web/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,15 +2,14 @@
/**
* storage handler
*/
import Storage from 'good-storage'
const SessionIdKey = 'ChatGPT_SESSION_ID';
export const Global = {}
export function getSessionId() {
return Storage.get(SessionIdKey)
return sessionStorage.getItem(SessionIdKey)
}
export function setSessionId(value) {
Storage.set(SessionIdKey, value)
sessionStorage.setItem(SessionIdKey, value)
}

View File

@@ -114,8 +114,6 @@ export default defineComponent({
this.chatBoxHeight = window.innerHeight - this.toolBoxHeight;
})
this.checkSession();
// for (let i = 0; i < 10; i++) {
// this.chatData.push({
// type: "prompt",
@@ -175,44 +173,11 @@ export default defineComponent({
this.chatBoxHeight = window.innerHeight - this.toolBoxHeight;
});
this.connect();
},
methods: {
// 检查会话
checkSession: function () {
httpPost("/api/session/get").then(() => {
if (this.socket == null) {
this.connect();
}
// 发送心跳
//setTimeout(() => this.checkSession(), 5000);
}).catch((res) => {
if (res.code === 400) {
this.showLoginDialog = true;
} else {
this.connectingMessageBox = ElMessageBox.confirm(
'^_^ 会话发生异常,您已经从服务器断开连接!',
'注意:',
{
confirmButtonText: '重连会话',
cancelButtonText: '不聊了',
type: 'warning',
showClose: false,
closeOnClickModal: false
}
).then(() => {
this.connect();
}).catch(() => {
ElMessage({
type: 'info',
message: '您关闭了会话',
})
})
}
})
},
connect: function () {
// 初始化 WebSocket 对象
const token = getSessionId();
@@ -264,8 +229,32 @@ export default defineComponent({
});
socket.addEventListener('close', () => {
// 检查会话,自动登录
this.checkSession();
// 检查会话
httpPost("/api/session/get").then(() => {
this.connectingMessageBox = ElMessageBox.confirm(
'^_^ 会话发生异常,您已经从服务器断开连接!',
'注意:',
{
confirmButtonText: '重连会话',
cancelButtonText: '不聊了',
type: 'warning',
showClose: false,
closeOnClickModal: false
}
).then(() => {
this.connect();
}).catch(() => {
ElMessage({
type: 'info',
message: '您关闭了会话',
})
})
}).catch((res) => {
if (res.code === 400) {
this.showLoginDialog = true;
}
})
});
this.socket = socket;