重构数据同步功能,新增客户端模式

This commit is contained in:
lyswhut
2023-02-28 18:01:56 +08:00
parent b42092884e
commit 3b70acc3ca
43 changed files with 2232 additions and 1249 deletions
+50 -17
View File
@@ -1,4 +1,4 @@
import { Socket as _Socket, RemoteSocket as _RemoteSocket } from 'socket.io'
import type WS from 'ws'
type DefaultEventsMap = Record<string, (...args: any[]) => void>
@@ -6,24 +6,57 @@ type DefaultEventsMap = Record<string, (...args: any[]) => void>
declare global {
namespace LX {
namespace Sync {
class Socket extends _Socket {
data: SocketData
namespace Client {
interface Socket extends WS.WebSocket {
isReady: boolean
data: {
keyInfo: ClientKeyInfo
urlInfo: UrlInfo
}
onRemoteEvent: <T extends keyof LX.Sync.ActionSyncSendType>(
eventName: T,
handler: (data: LX.Sync.ActionSyncSendType[T]) => (void | Promise<void>)
) => () => void
onClose: (handler: (err: Error) => (void | Promise<void>)) => () => void
sendData: <T extends keyof LX.Sync.ActionSyncType>(
eventName: T,
data?: LX.Sync.ActionSyncType[T],
callback?: (err?: Error) => void
) => void
}
interface UrlInfo {
wsProtocol: string
httpProtocol: string
hostPath: string
href: string
}
}
class RemoteSocket extends _RemoteSocket<DefaultEventsMap, any> {
readonly data: SocketData
namespace Server {
interface Socket extends WS.WebSocket {
isAlive?: boolean
isMobile: boolean
isReady: boolean
keyInfo: LX.Sync.ServerKeyInfo
onRemoteEvent: <T extends keyof LX.Sync.ActionSyncType>(
eventName: T,
handler: (data: LX.Sync.ActionSyncType[T]) => void
) => () => void
onClose: (handler: (err: Error) => (void | Promise<void>)) => () => void
sendData: <T extends keyof LX.Sync.ActionSyncSendType>(
eventName: T,
data?: LX.Sync.ActionSyncSendType[T],
callback?: (err?: Error) => void
) => void
}
type SocketServer = WS.Server<Socket>
}
interface Data {
action: string
data: any
}
interface SocketData {
snapshotFilePath: string
isCreatedSnapshot: boolean
keyInfo: KeyInfo
isReady: boolean
}
type Action = 'list:sync'
type ListAction = 'getData' | 'finished'
}
}