完善同步

pull/1094/head
lyswhut 2022-12-30 17:06:03 +08:00
parent 5f11fa65b4
commit 3a9dbbb577
4 changed files with 12 additions and 4 deletions

View File

@ -138,7 +138,7 @@ const broadcast = async(action: listAction, data: any, excludeIds: string[] = []
if (!io) return
const sockets: LX.Sync.RemoteSocket[] = await io.fetchSockets()
for (const socket of sockets) {
if (excludeIds.includes(socket.data.keyInfo.clientId)) continue
if (excludeIds.includes(socket.data.keyInfo.clientId) || !socket.data.isReady) continue
socket.emit(action, encryptMsg(socket.data.keyInfo, data))
}
}
@ -150,10 +150,13 @@ export const sendListAction = async(action: LX.Sync.ActionList) => {
}
export const registerListHandler = (_io: Server, socket: LX.Sync.Socket) => {
unregisterListHandler()
if (!io) {
io = _io
removeListener = registerListActionEvent()
}
io = _io
socket.on('list:action', msg => {
if (!socket.data.isReady) return
// console.log(msg)
msg = decryptMsg(socket.data.keyInfo, msg)
if (!msg) return
@ -162,7 +165,6 @@ export const registerListHandler = (_io: Server, socket: LX.Sync.Socket) => {
// socket.broadcast.emit('list:action', { action: 'list_remove', data: { id: 'default', index: 0 } })
})
removeListener = registerListActionEvent()
// socket.on('list:add', addMusic)
}
export const unregisterListHandler = () => {

View File

@ -121,6 +121,7 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje
setClientKeyInfo(keyInfo)
// socket.lx_keyInfo = keyInfo
socket.data.keyInfo = keyInfo
socket.data.isReady = false
try {
await syncList(io, socket)
} catch (err) {
@ -130,6 +131,7 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje
}
status.devices.push(keyInfo)
handleConnection(io, socket)
socket.data.isReady = true
sendStatus(status)
})

View File

@ -22,6 +22,8 @@ const patchListData = (listData: Partial<LX.Sync.ListData>): LX.Sync.ListData =>
const getRemoteListData = async(socket: LX.Sync.Socket): Promise<LX.Sync.ListData> => await new Promise((resolve, reject) => {
console.log('getRemoteListData')
const handleError = (reason: string) => {
socket.removeListener('list:sync', handleSuccess)
socket.removeListener('disconnect', handleError)
reject(new Error(reason))
}
const handleSuccess = (enData: string) => {
@ -449,6 +451,7 @@ const syncList = async(socket: LX.Sync.Socket): Promise<LX.Sync.ListData | null>
const checkSyncQueue = async(): Promise<void> => {
if (!syncingId) return
console.log('sync queue...')
await wait()
return await checkSyncQueue()
}

View File

@ -20,6 +20,7 @@ declare global {
snapshotFilePath: string
isCreatedSnapshot: boolean
keyInfo: KeyInfo
isReady: boolean
}
type Action = 'list:sync'
type ListAction = 'getData' | 'finished'