feat: add server and frontend initial - wood chopping
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { PROTOCOL_VERSION, type ClientMessage, type ServerMessage } from '@idle/shared';
|
||||
import { PROTOCOL_VERSION, type ClientMessage, type DeltaMsg, type ServerMessage, type SnapshotMsg, type WelcomeMsg } from '@idle/shared';
|
||||
|
||||
export type Snapshot = Extract<ServerMessage, { t: 'snapshot' }>;
|
||||
export type NetStatus = 'connecting' | 'online' | 'reconnecting';
|
||||
|
||||
export function wsUrl(): string {
|
||||
@@ -13,7 +12,9 @@ export function wsUrl(): string {
|
||||
|
||||
export interface ConnectHandlers {
|
||||
onStatus: (status: NetStatus) => void;
|
||||
onSnapshot: (snap: Snapshot) => void;
|
||||
onWelcome: (msg: WelcomeMsg) => void;
|
||||
onSnapshot: (msg: SnapshotMsg) => void;
|
||||
onDelta: (msg: DeltaMsg) => void;
|
||||
}
|
||||
|
||||
/** Подключение к миру с автопереподключением; возвращает функцию закрытия. */
|
||||
@@ -41,8 +42,11 @@ export function connectWorld(handlers: ConnectHandlers): () => void {
|
||||
}
|
||||
if (msg.t === 'welcome') {
|
||||
handlers.onStatus('online');
|
||||
handlers.onWelcome(msg);
|
||||
} else if (msg.t === 'snapshot') {
|
||||
handlers.onSnapshot(msg);
|
||||
} else if (msg.t === 'delta') {
|
||||
handlers.onDelta(msg);
|
||||
}
|
||||
};
|
||||
ws.onclose = () => {
|
||||
|
||||
Reference in New Issue
Block a user