|
| 1 | +Network Overview |
| 2 | +================ |
| 3 | + |
| 4 | +This page describes the global design and rationale for the engine's networking |
| 5 | +libraries: the `network-server` and `network-client` TypeScript packages. These |
| 6 | +libraries provide a small, pragmatic networking layer used by the example |
| 7 | +`pong-network` game and designed to be easy to understand and integrate. |
| 8 | + |
| 9 | +Goals |
| 10 | +----- |
| 11 | + |
| 12 | +- Minimal, predictable protocol for multiplayer games. |
| 13 | +- Use well-understood transports: TCP for reliable control messages and UDP |
| 14 | + for low-latency, best-effort state updates. |
| 15 | +- Provide clear validation hooks (magic value, versioning) to avoid processing |
| 16 | + ```restructuredtext |
| 17 | + Network Overview |
| 18 | + ================ |
| 19 | +
|
| 20 | + This page explains how the engine's TypeScript networking packages actually |
| 21 | + work and the rationale behind important implementation choices. The two |
| 22 | + packages are `network-server` and `network-client` and are used by the |
| 23 | + `example/pong-network` project. |
| 24 | +
|
| 25 | + Design summary |
| 26 | + -------------- |
| 27 | +
|
| 28 | + - Two logical transports are provided: a reliable, ordered channel (called |
| 29 | + "TCP" in the packages) and an unreliable, unordered channel (called |
| 30 | + "UDP"). Important: these names refer to channel semantics in the library, |
| 31 | + not to raw OS sockets. Implementation details: |
| 32 | + - The reliable channel is implemented over WebSocket (node `ws` and |
| 33 | + browser `WebSocket`) to provide an ordered, byte-stream-like channel. |
| 34 | + - The unreliable channel is implemented as a WebRTC `RTCDataChannel` |
| 35 | + created with `ordered: false, maxRetransmits: 0`. WebSocket is used for |
| 36 | + signaling/ICE exchange between client and server. |
| 37 | +
|
| 38 | +- For packet framing and terminator semantics see the dedicated note: |
| 39 | + `docs/network/packet-framing.rst`. |
| 40 | +
|
| 41 | + Why these choices |
| 42 | + ------------------ |
| 43 | +
|
| 44 | + - WebSocket for reliable messages: WebSocket is universally available in |
| 45 | + browsers and easy to host in Node.js. Using it for the "TCP" channel avoids |
| 46 | + needing a separate TCP server and simplifies browser + native client parity. |
| 47 | +
|
| 48 | + - WebRTC DataChannel for unreliable messages: Browsers cannot open raw UDP |
| 49 | + sockets; WebRTC provides a browser-friendly unreliable datagram channel |
| 50 | + with low latency. The repository uses WebSocket for ICE signaling and |
| 51 | + negotiates a `RTCDataChannel` for actual game-state messages. |
| 52 | +
|
| 53 | + - Terminator (magic value) appended at packet end: appending a terminator is |
| 54 | + robust against fragmented transport frames. Because WebSocket and RTC |
| 55 | + DataChannels can split or aggregate application messages, a terminator |
| 56 | + allows the receiver to detect full logical packets regardless of chunking. |
| 57 | +
|
| 58 | + - Configurable `magicValue`: The default (`PACKET_END`) is a human-readable |
| 59 | + sentinel that makes debugging easier; it is configurable via the server or |
| 60 | + client config objects if you prefer a shorter or binary marker. |
| 61 | +
|
| 62 | + Serialization and extensibility |
| 63 | + ------------------------------- |
| 64 | +
|
| 65 | + - Example code uses JSON payloads for clarity (easy to inspect and debug). |
| 66 | + The transport layer operates on `Uint8Array` buffers, so you can replace |
| 67 | + JSON with any binary encoding for |
| 68 | + production. |
0 commit comments