2026-02-21 16:42:42 -05:00
# Runtime Flow
## Connect Flow
1. User clicks connect.
2026-02-28 04:29:57 -05:00
2. Client validates auth form and sets up local media.
2026-03-08 20:51:50 -04:00
3. Client connects signaling websocket from the configured app origin.
4. Server accepts the socket only when the browser `Origin` matches `CHGRID_HOST_ORIGIN` , then attempts cookie-based session resume from websocket handshake cookie (`chgrid_session_token` ).
2026-02-28 04:29:57 -05:00
5. If resume does not authenticate, server sends `auth_required` .
2026-02-24 22:35:29 -05:00
- includes `authPolicy` limits for username/password.
2026-02-28 04:29:57 -05:00
6. Client sends `auth_login` or `auth_register` (or explicit `auth_resume` if provided by caller).
7. Server sends `auth_result` .
2026-02-27 03:37:20 -05:00
- includes role + permissions for authenticated session.
2026-02-28 04:29:57 -05:00
8. Client persists authenticated session into a server-managed `HttpOnly` cookie via `GET /auth/session/set` (`Authorization: Bearer <sessionToken>` , `X-Chgrid-Auth-Client: 1` ), and clears it via `GET /auth/session/clear` (`X-Chgrid-Auth-Client: 1` ) on logout/session errors.
2026-03-08 21:44:27 -04:00
- the optional PHP media proxy validates that same cookie through `GET /auth/session/check` before relaying media
2026-02-28 04:29:57 -05:00
9. Server sends `welcome` with users/items snapshot.
10. Client:
2026-02-21 19:12:58 -05:00
- applies `welcome.worldConfig.gridSize` for authoritative grid bounds/rendering
2026-02-24 19:52:38 -05:00
- applies `welcome.worldConfig.movementTickMs` as movement pacing guidance
- applies `welcome.worldConfig.movementMaxStepsPerTick` for movement-rate parity
2026-02-24 23:37:10 -05:00
- uses `welcome.player` as authoritative starting position (restored from server-side account state when available)
2026-02-22 18:20:13 -05:00
- records `welcome.serverInfo` (`instanceId` , `version` ) for restart detection
2026-02-22 18:40:26 -05:00
- if `welcome.serverInfo.version` differs from running client version, auto-reloads the page
2026-03-08 19:35:04 -04:00
- applies `welcome.uiDefinitions` for item menus/properties/options, server-backed command metadata, item-management metadata, and admin menu labels/order
2026-02-24 19:52:38 -05:00
- sends initial `update_position` echo from server-assigned starting tile
2026-02-21 16:42:42 -05:00
- sends initial `update_nickname`
- creates peer runtimes for known users
- syncs item runtimes (`radio` , `emit` )
- applies audio layer state
2026-02-22 18:20:13 -05:00
- starts signaling heartbeat monitor
2026-02-21 16:42:42 -05:00
- starts game loop
## Main Loop
Each frame:
- Handle local movement input.
2026-02-24 19:52:38 -05:00
- Send movement intents; server remains authoritative on accepted movement updates.
2026-02-21 16:42:42 -05:00
- Update spatial voice audio.
- Update spatial radio audio.
- Update spatial item emit audio.
- Draw canvas scene.
## Message Handling
Core incoming message effects:
- `signal` : WebRTC negotiation and ICE exchange.
2026-02-24 22:03:10 -05:00
- `auth_required` : prompt client to authenticate before gameplay messages.
2026-02-24 22:35:29 -05:00
- `auth_result` : auth success/failure with optional session token + account metadata + `authPolicy` .
2026-02-27 03:37:20 -05:00
- `auth_permissions` : live permission refresh (role + permission set) after role/permission admin changes.
- `admin_roles_list` : role metadata + user counts + permission keys for role management UI.
- `admin_users_list` : user metadata list for role/ban admin flows.
- `admin_action_result` : success/error for role/user admin mutations.
2026-02-21 16:42:42 -05:00
- `update_position` : update peer position; may play movement/teleport world sound.
2026-02-24 20:55:02 -05:00
- `teleport_complete` : play peer teleport landing sound at final tile.
2026-02-21 16:42:42 -05:00
- `update_nickname` : update peer display name.
- `chat_message` : append/readable status; optional system sound class.
- `item_upsert` : replace item snapshot and resync item runtimes.
- `item_remove` : remove item and cleanup runtimes.
- `item_action_result` : success/error status for actions.
- `item_use_sound` : play one-shot spatial sample (world layer gated).
2026-02-22 23:42:17 -05:00
- `item_piano_note` : start/stop synthesized piano notes from remote users (item layer gated).
2026-02-24 19:56:44 -05:00
- `item_piano_status` : structured piano mode/record/playback transitions (client runtime state).
2026-02-22 18:20:13 -05:00
- `pong` :
- positive `clientSentAt` : user ping response (`P` command)
- negative `clientSentAt` : internal heartbeat response
## Stale Connection Recovery
2026-02-22 18:47:09 -05:00
- If websocket closes unexpectedly, client starts reconnect flow immediately.
- While running, client also sends heartbeat `ping` every 10 seconds (fallback for silent half-open cases).
- If one heartbeat `pong` is missed (10-second interval), client starts reconnect flow.
2026-02-22 18:52:06 -05:00
- Reconnect flow waits 5 seconds and retries up to 3 times.
2026-02-22 18:40:26 -05:00
- If reconnect lands on a different `welcome.serverInfo.instanceId` , client announces server restart.
- Connect/reconnect status message is emitted from `welcome` and includes server version.
2026-02-21 16:42:42 -05:00
2026-02-27 03:37:20 -05:00
## Authorization Runtime
- Server enforces item/chat/nickname/voice/admin permissions for each packet.
- Role and permission changes apply live to connected users without reconnect.
- `voice.send` revocation is pushed immediately via `auth_permissions` ; client mutes outbound voice track.
2026-02-21 16:42:42 -05:00
## Disconnect/Cleanup
On disconnect:
- Close signaling.
2026-02-22 18:20:13 -05:00
- Stop heartbeat monitor.
2026-02-21 16:42:42 -05:00
- Stop local media tracks.
- Cleanup peers and all audio runtimes.
- Reset UI/mode state and lists.
## Runtime Components
- `PeerManager` : peer connection lifecycle and remote track attach.
- `RadioStationRuntime` : shared stream sources + per-item output/effects/spatialization.
- `ItemEmitRuntime` : per-item looping emit source + spatialization.
- `AudioEngine` : shared audio context, samples, effects, voice graph.