NetworkClientLibrary, from @nanoforge-dev/network/client, connects the game client to a server. See Network to register it and set its environment variables.
Overview
A client connects to a single server instance and sends every packet on one of four channels, picked with thechannel option:
Channel.ReliableOrdered(the default) andChannel.ReliableUnordered, carried by a TCP WebSocket, for control commands such as join, play, and input.Channel.UnreliableOrderedandChannel.UnreliableUnordered, carried by WebRTC data channels, for server snapshots or low-latency updates.
Example
Typed packets
Instead of switching on atype field, declare each packet type once, in a file
both the client and the server import, and let the library route packets to it.
- Listeners are called every tick before the ECS systems, in the order the packets arrived, reliable channels first. They are not called while the game is paused: packets wait for the next tick.
readreturns the packets of the current tick; a system that does not read them every tick misses some.- Typed packets never appear in
getReceivedPackets, which only returns the packets sent withsendData. - A packet type with no listener and no reader is dropped with a warning, printed once per type.
Notes
- See Network Client API for the full list of available functions, and for how each kind of payload is encoded.
- For the channels and how packets are framed on each, see Packet Channels.