NetworkManager
NetworkManager handles the client-side WebRTC connection to the server via geckos.io.
Creating and getting the instance
Subclass it with geckos.io client options. When the game server serves the client, connect to the page's own origin and port, like the multiplayer template does:
import BaseNetworkManager from '@mavonengine/core/Networking/Client/NetworkManager'
export default class NetworkManager extends BaseNetworkManager {
constructor() {
super({
// @ts-expect-error null uses the page's own port
port: null,
url: '/socket',
})
}
}
NetworkManager is a singleton:
const network = NetworkManager.getInstance()
// Also set on the game, for dev tools
Game.instance().network
Properties
| Property | Type | Description |
|---|---|---|
socket | geckos.io socket | The underlying WebRTC socket |
ping | number | Current round-trip latency in milliseconds |
connected | boolean | Whether the client is connected to the server |
editorMode | boolean | Whether this client runs the editor, and so is no player on the server |
identity | Identity | undefined | Who the server's auth plugin let this client in as, once connected. See Authentication |
connectError | ConnectError | null | Why the last attempt to connect failed, null before and once connected |
serverView | ServerView | What the server last sent, untouched by interpolation or prediction, see below |
net | NetRecorder | The last packets and network events of this client, for the dev tools. See network debugging |
Sending commands to the server
Send commands through sendCommand. It assigns the next sequence id and keeps the command in a local queue for replay:
network.sendCommand({
type: ClientCommand.CL_MOVE,
keys: [...input.keysPressed],
yaw,
})
Reconciliation
The server sends each player's lastProcessedSequenceId in the state sync. Drop the commands the server has already applied; the ones left in the queue are those the server hasn't seen yet, which you can replay on top of the server's position:
network.dropCommandsAtSequenceId(player.lastProcessedSequenceId)
Receiving state from the server
Listen on the socket once connected, e.g. in onConnect():
protected override onConnect() {
super.onConnect()
this.socket.on(ServerCommand.SV_STATE, (data) => {
// Apply server state to local entities, see State Sync
})
}
The server's view
network.serverView keeps what the last state update said about itself:
| Field | Description |
|---|---|
tick | Server tick of the last state, null until the first one arrived |
serverTime | Epoch ms on the server when the state was taken |
ack | Last input of this client the server had processed, null for the editor |
receivedAt | performance.now() when it arrived |
missedStates | States that never arrived, counted from gaps in the tick numbers |
entities | Raw server state per entity id. Only filled while net records, for the dev tools |
A state that arrives after a newer one is ignored here: the data channel is unordered.
Authentication
When the server has an auth plugin such as @mavonengine/auth, the connection request carries an Authorization header for it to check. The plugin's client entry gives it, so a game normally passes nothing. A game that handles the token itself passes authorization in the options:
super({
port: null,
url: '/socket',
authorization: `Bearer ${token}`,
})
The token is read when the NetworkManager is made, and the client does not try again after a refusal: make a new one to connect again after logging in or out.
Once connected, network.identity is who the server let this client in as ({ id, name?, … }). It is undefined when the server has no auth plugin or let the client in without an identity.
A refusal arrives as the connectError event and the onConnectError() hook, with the HTTP status the server answered:
network.on('connectError', (error) => {
if (error.status === 401)
showLogin() // no session that works
})
status | |
|---|---|
401 | No session that works: log in again |
403 | The account may not connect, e.g. it is disabled |
503 | The server has an auth plugin that isn't running, so it turns every client away |
| none | The connection failed for another reason, see message |
network.connectError keeps the last one for whoever looks after it happened.
Editor mode
When the level editor boots, the connection stays open and the client announces itself as an editor. The server then removes its player and sends it the whole world, so other players keep showing while you edit.
Connection events
Override onConnect(), onDisconnect(), onConnectError() and onPong() in your subclass to react to the connection. onConnect() must call super.onConnect(), which starts the ping checks. NetworkManager extends EventEmitter, so you can trigger() what you receive for the rest of your game to listen to.