NetworkServerLibrary, from @nanoforge-dev/network/server, accepts client connections in the game server. See Network to register it and set its environment variables.
Overview
The server listens on configured ports and accepts client connections. A single server process can accept many clients, and typical responsibilities in a game are:- Accept reliable control messages from clients on
Channel.ReliableOrderedandChannel.ReliableUnordered, carried by a TCP WebSocket. - Optionally establish
RTCPeerConnections via WebSocket signaling forChannel.UnreliableOrderedandChannel.UnreliableUnordered, carried by WebRTC data channels, for low-latency state updates.
Client identification
Every client gets aClientId: a random string (UUID) shared by the TCP and UDP
transports.
- The first transport a client opens starts a session. The server answers with a
welcomemessage carrying the id and a secret session token (a text frame on TCP, where game data is always binary, and a JSON message on the UDP signaling socket). - The client presents that token (
?token=...) when it opens its other transport, which joins the same session. An unknown token, or a second connection on a transport the session already has, is rejected with401. - The session ends when its last transport closes.
ClientId.
onConnect fires as soon as the first transport is up, so when both transports
are used, UDP may not be linked yet.
Example
Typed packets
The server uses the same packet types as the client, from the shared protocol file (see Typed packets). Its listeners andread also get the sender.
validate option of definePacket.
Notes
- See Network Server API for the full list of available functions.
- For the channels and how packets are framed on each, see Packet Channels.