完善同步
parent
5f11fa65b4
commit
3a9dbbb577
|
@ -138,7 +138,7 @@ const broadcast = async(action: listAction, data: any, excludeIds: string[] = []
|
||||||
if (!io) return
|
if (!io) return
|
||||||
const sockets: LX.Sync.RemoteSocket[] = await io.fetchSockets()
|
const sockets: LX.Sync.RemoteSocket[] = await io.fetchSockets()
|
||||||
for (const socket of sockets) {
|
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))
|
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) => {
|
export const registerListHandler = (_io: Server, socket: LX.Sync.Socket) => {
|
||||||
unregisterListHandler()
|
if (!io) {
|
||||||
|
io = _io
|
||||||
|
removeListener = registerListActionEvent()
|
||||||
|
}
|
||||||
|
|
||||||
io = _io
|
|
||||||
socket.on('list:action', msg => {
|
socket.on('list:action', msg => {
|
||||||
|
if (!socket.data.isReady) return
|
||||||
// console.log(msg)
|
// console.log(msg)
|
||||||
msg = decryptMsg(socket.data.keyInfo, msg)
|
msg = decryptMsg(socket.data.keyInfo, msg)
|
||||||
if (!msg) return
|
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 } })
|
// socket.broadcast.emit('list:action', { action: 'list_remove', data: { id: 'default', index: 0 } })
|
||||||
})
|
})
|
||||||
|
|
||||||
removeListener = registerListActionEvent()
|
|
||||||
// socket.on('list:add', addMusic)
|
// socket.on('list:add', addMusic)
|
||||||
}
|
}
|
||||||
export const unregisterListHandler = () => {
|
export const unregisterListHandler = () => {
|
||||||
|
|
|
@ -121,6 +121,7 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje
|
||||||
setClientKeyInfo(keyInfo)
|
setClientKeyInfo(keyInfo)
|
||||||
// socket.lx_keyInfo = keyInfo
|
// socket.lx_keyInfo = keyInfo
|
||||||
socket.data.keyInfo = keyInfo
|
socket.data.keyInfo = keyInfo
|
||||||
|
socket.data.isReady = false
|
||||||
try {
|
try {
|
||||||
await syncList(io, socket)
|
await syncList(io, socket)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -130,6 +131,7 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje
|
||||||
}
|
}
|
||||||
status.devices.push(keyInfo)
|
status.devices.push(keyInfo)
|
||||||
handleConnection(io, socket)
|
handleConnection(io, socket)
|
||||||
|
socket.data.isReady = true
|
||||||
sendStatus(status)
|
sendStatus(status)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -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) => {
|
const getRemoteListData = async(socket: LX.Sync.Socket): Promise<LX.Sync.ListData> => await new Promise((resolve, reject) => {
|
||||||
console.log('getRemoteListData')
|
console.log('getRemoteListData')
|
||||||
const handleError = (reason: string) => {
|
const handleError = (reason: string) => {
|
||||||
|
socket.removeListener('list:sync', handleSuccess)
|
||||||
|
socket.removeListener('disconnect', handleError)
|
||||||
reject(new Error(reason))
|
reject(new Error(reason))
|
||||||
}
|
}
|
||||||
const handleSuccess = (enData: string) => {
|
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> => {
|
const checkSyncQueue = async(): Promise<void> => {
|
||||||
if (!syncingId) return
|
if (!syncingId) return
|
||||||
|
console.log('sync queue...')
|
||||||
await wait()
|
await wait()
|
||||||
return await checkSyncQueue()
|
return await checkSyncQueue()
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ declare global {
|
||||||
snapshotFilePath: string
|
snapshotFilePath: string
|
||||||
isCreatedSnapshot: boolean
|
isCreatedSnapshot: boolean
|
||||||
keyInfo: KeyInfo
|
keyInfo: KeyInfo
|
||||||
|
isReady: boolean
|
||||||
}
|
}
|
||||||
type Action = 'list:sync'
|
type Action = 'list:sync'
|
||||||
type ListAction = 'getData' | 'finished'
|
type ListAction = 'getData' | 'finished'
|
||||||
|
|
Loading…
Reference in New Issue