fixed bug for websocket message handler rebind

This commit is contained in:
RockYang 2024-10-08 16:41:19 +08:00
parent 8498cd71dc
commit c374126f69
4 changed files with 35 additions and 50 deletions

View File

@ -54,7 +54,7 @@ onMounted(() => {
}) })
watch(() => store.isLogin, (val) => { watch(() => store.isLogin, (val) => {
if (val && store.socket.readyState !== WebSocket.OPEN) { if (val) {
connect() connect()
} }
}) })
@ -79,26 +79,11 @@ const connect = () => {
_socket.send(JSON.stringify({"type":"ping"})) _socket.send(JSON.stringify({"type":"ping"}))
} }
},5000) },5000)
//
for (const key in store.messageHandlers) {
console.log(store.messageHandlers[key])
store.setMessageHandler(store.messageHandlers[key])
}
}) })
_socket.addEventListener('close', () => { _socket.addEventListener('close', () => {
//
for (const key in store.messageHandlers) {
if (store.socket) {
store.socket.removeEventListener('message', store.messageHandlers[key])
}
}
store.setSocket(null)
clearInterval(handler.value) clearInterval(handler.value)
connect() connect()
}); });
store.setSocket(_socket) store.setSocket(_socket)
} }

View File

@ -6,8 +6,7 @@ export const useSharedStore = defineStore('shared', {
showLoginDialog: false, showLoginDialog: false,
chatListStyle: Storage.get("chat_list_style","chat"), chatListStyle: Storage.get("chat_list_style","chat"),
chatStream: Storage.get("chat_stream",true), chatStream: Storage.get("chat_stream",true),
socket: WebSocket, socket: {conn:null, handlers:{}},
messageHandlers:{},
mobileTheme: Storage.get("mobile_theme", "light"), mobileTheme: Storage.get("mobile_theme", "light"),
adminTheme: Storage.get("admin_theme", "light"), adminTheme: Storage.get("admin_theme", "light"),
isLogin: false isLogin: false
@ -26,18 +25,23 @@ export const useSharedStore = defineStore('shared', {
Storage.set("chat_stream", value); Storage.set("chat_stream", value);
}, },
setSocket(value) { setSocket(value) {
this.socket = value; for (const key in this.socket.handlers) {
this.setMessageHandler(value, this.socket.handlers[key])
}
this.socket.conn = value
}, },
addMessageHandler(key, callback) { addMessageHandler(key, callback) {
if (!this.messageHandlers[key]) { if (!this.socket.handlers[key]) {
this.setMessageHandler(callback) this.socket.handlers[key] = callback;
} }
this.messageHandlers[key] = callback; this.setMessageHandler(this.socket.conn, callback)
}, },
setMessageHandler(callback) {
if (this.socket instanceof WebSocket && this.socket.readyState === WebSocket.OPEN) { setMessageHandler(conn, callback) {
console.log(callback) if (!conn) {
this.socket.addEventListener('message', (event) => { return
}
conn.addEventListener('message', (event) => {
try { try {
if (event.data instanceof Blob) { if (event.data instanceof Blob) {
const reader = new FileReader(); const reader = new FileReader();
@ -50,17 +54,13 @@ export const useSharedStore = defineStore('shared', {
console.warn(e) console.warn(e)
} }
}) })
} else {
setTimeout(() => {
this.setMessageHandler(callback)
}, 1000)
}
}, },
removeMessageHandler(key) { removeMessageHandler(key) {
if (this.socket.readyState === WebSocket.OPEN) { if (this.socket.conn && this.socket.conn.readyState === WebSocket.OPEN) {
this.socket.removeEventListener('message', this.messageHandlers[key]) this.socket.conn.removeEventListener('message', this.socket.handlers[key])
} }
delete this.messageHandlers[key] delete this.socket.handlers[key]
}, },
setMobileTheme(theme) { setMobileTheme(theme) {
this.mobileTheme = theme this.mobileTheme = theme

View File

@ -689,7 +689,7 @@ const sendMessage = function () {
return; return;
} }
if (store.socket.readyState !== WebSocket.OPEN) { if (store.socket.conn.readyState !== WebSocket.OPEN) {
ElMessage.warning("连接断开,正在重连..."); ElMessage.warning("连接断开,正在重连...");
return return
} }
@ -727,7 +727,7 @@ const sendMessage = function () {
showHello.value = false showHello.value = false
disableInput(false) disableInput(false)
store.socket.send(JSON.stringify({ store.socket.conn.send(JSON.stringify({
channel: 'chat', channel: 'chat',
type:'text', type:'text',
body:{ body:{
@ -825,7 +825,7 @@ const reGenerate = function (prompt) {
icon: loginUser.value.avatar, icon: loginUser.value.avatar,
content: text content: text
}); });
store.socket.send(JSON.stringify({ store.socket.conn.send(JSON.stringify({
channel: 'chat', channel: 'chat',
type:'text', type:'text',
body:{ body:{

View File

@ -512,7 +512,7 @@ const sendMessage = () => {
return return
} }
if (store.socket.readyState !== WebSocket.OPEN) { if (store.socket.conn.readyState !== WebSocket.OPEN) {
showToast("连接断开,正在重连..."); showToast("连接断开,正在重连...");
return return
} }
@ -536,7 +536,7 @@ const sendMessage = () => {
}) })
disableInput(false) disableInput(false)
store.socket.send(JSON.stringify({ store.socket.conn.send(JSON.stringify({
channel: 'chat', channel: 'chat',
type:'text', type:'text',
body:{ body:{
@ -569,7 +569,7 @@ const reGenerate = () => {
icon: loginUser.value.avatar, icon: loginUser.value.avatar,
content: renderInputText(text) content: renderInputText(text)
}); });
store.socket.send(JSON.stringify({ store.socket.conn.send(JSON.stringify({
channel: 'chat', channel: 'chat',
type:'text', type:'text',
body:{ body:{