接入群消息实时提醒和重连补拉

This commit is contained in:
xhgit committed 2026-11-14 14:59:24 +08:00
1 parent 5ace82b0d7
commit b2e04e8174
10 files changed
+122 -33

No files matched your search

+5 -2
View File
@@ -26,6 +26,7 @@ export const useSession = defineStore('session', () => {
const connection = ref('离线')
const contactChanges = ref(0)
const inboxChanges = ref(0)
const groupChange = shallowRef<{ groupId: string }>()
let token = ''
let socket: WebSocket | undefined
let retry: ReturnType<typeof setTimeout> | undefined
@@ -34,6 +35,7 @@ export const useSession = defineStore('session', () => {
function clear() {
token = ''
identity.value = undefined
groupChange.value = undefined
connection.value = '离线'
clearTimeout(retry)
const previous = socket
@@ -51,13 +53,14 @@ export const useSession = defineStore('session', () => {
socket = active
active.onmessage = (event) => {
if (socket !== active) return
const { type } = JSON.parse(event.data)
const { type, groupId } = JSON.parse(event.data)
if (type === 'ready') {
connection.value = '实时连接已建立'
attempts = 0
}
if (type === 'contacts_changed') contactChanges.value++
if (type === 'inbox_changed') inboxChanges.value++
if (type === 'group_changed' && typeof groupId === 'string') groupChange.value = { groupId }
}
active.onclose = (event) => {
if (socket !== active) return
@@ -109,7 +112,7 @@ export const useSession = defineStore('session', () => {
}
return {
identity, connection, contactChanges, inboxChanges, authenticate, logout, authorized,
identity, connection, contactChanges, inboxChanges, groupChange, authenticate, logout, authorized,
devices: () => authorized<Device[]>('/devices'),
revoke: (id: string) => authorized<void>('/devices/' + encodeURIComponent(id), 'DELETE'),
contacts: () => authorized<Contact[]>('/contacts'),