Skip to main content

Class: Network

Constructors​

constructor​

• new Network(options?): Network

Parameters​

NameType
optionsPartial<NetworkOptions>

Returns​

Network

Properties​

clientInfo​

• clientInfo: Object

Type declaration​

NameType
idstring
metadata?Record<string, any>
usernamestring

connected​

• connected: boolean = false


disconnectReason​

• disconnectReason: string = ""


intercepts​

• intercepts: NetIntercept[] = []


joined​

• joined: boolean = false


onConnect​

• onConnect: () => void

Type declaration​

▸ (): void

Returns​

void


onDisconnect​

• onDisconnect: () => void

Type declaration​

▸ (): void

Returns​

void


onJoin​

• onJoin: (world: string) => void

Type declaration​

▸ (world): void

Parameters​
NameType
worldstring
Returns​

void


onLeave​

• onLeave: (world: string) => void

Type declaration​

▸ (world): void

Parameters​
NameType
worldstring
Returns​

void


options​

• options: NetworkOptions


socket​

• socket: URL


url​

• url: Url<{ [key: string]: any; }>


world​

• world: string


ws​

• ws: ProtocolWS = null

Accessors​

concurrentWorkers​

• get concurrentWorkers(): number

Returns​

number


droppedCommandCount​

• get droppedCommandCount(): number

Command packets dropped for good, with an error logged for each batch.

Returns​

number


droppedPacketCount​

• get droppedPacketCount(): number

Inbound packets dropped unprocessed this session, each reported in an error log (see NetworkOptions.maxQueuedPackets).

Returns​

number


isClientOutdated​

• get isClientOutdated(): boolean

Terminal protocol rejection: only a fresh client build can reconnect.

Returns​

boolean


isJoinPending​

• get isJoinPending(): boolean

True between a (re)join request and its INIT: reads of world state are answered from a map the server may no longer agree with.

Returns​

boolean


joinGeneration​

• get joinGeneration(): number

Completed INIT handshakes so far; bumps on first join, every rejoin, and every world switch.

Returns​

number


packetQueueLength​

• get packetQueueLength(): number

Returns​

number


pendingCommandCount​

• get pendingCommandCount(): number

Command packets waiting for a live session to retry on.

Returns​

number


rtcConnected​

• get rtcConnected(): boolean

Returns​

boolean


serverUrl​

• get serverUrl(): string

Returns​

string

Methods​

action​

▸ action(type, data?): Promise<void>

Parameters​

NameType
typestring
data?any

Returns​

Promise<void>


connect​

▸ connect(serverURL, options?): Promise<Network>

Parameters​

NameType
serverURLstring
optionsNetworkConnectionOptions

Returns​

Promise<Network>


connectWebRTC​

▸ connectWebRTC(): Promise<void>

Returns​

Promise<void>


disconnect​

▸ disconnect(): void

Returns​

void


flush​

▸ flush(): void

Returns​

void


isPacketPendingSend​

▸ isPacketPendingSend(packet): boolean

Whether this exact packet object is still waiting in the command retry queue. Together with the packet's absence from its intercept queue this lets a caller prove a command was handed to an OPEN socket.

Parameters​

NameType
packetMessageProtocol

Returns​

boolean


join​

▸ join(world): Promise<Network>

Parameters​

NameType
worldstring

Returns​

Promise<Network>


leave​

▸ leave(): void

Returns​

void


offerRegistry​

▸ offerRegistry(entry): boolean

Hand the network a block registry from elsewhere (an HTTP fetch made while the page loaded, say) for the next JOIN to name, exactly as a registry read back from storage is. The digest must be the one the server gave with that text; the network never computes one.

Taken only while it can still serve a JOIN: before one is sent, it replaces whatever storage had brought back (a fetch answers for the server's registry now, storage for an earlier one); with a JOIN in flight, only the registry that JOIN named. Returns whether the network now holds this registry.

Parameters​

NameType
entryCachedRegistry

Returns​

boolean


parseHeldRegistry​

▸ parseHeldRegistry(): ParsedRegistry

The registry this client holds for its next INIT, parsed now, once, instead of when that INIT arrives. An INIT that leaves the registry out is then completed with these very objects, so anything built from them ahead of it (the world loading its blocks early) is built from what the INIT carries. Null while no registry is held, or when its text does not parse. Call it before the INIT lands; the objects belong to that INIT.

Returns​

ParsedRegistry


reconnectNow​

▸ reconnectNow(): boolean

Trigger an immediate reconnect attempt, bypassing the periodic backoff. Returns false when there is nothing to do: already connected, never connected, or terminally rejected (outdated client build).

Returns​

boolean


register​

▸ register(...intercepts): Network

Parameters​

NameType
...interceptsNetIntercept[]

Returns​

Network


send​

▸ send(event): boolean

Hand one event to the socket. Returns whether the packet was actually given to an OPEN socket: false means it was NOT sent (no socket, still connecting, closing, or closed). Callers that carry one-shot intent must check the answer; flush does this for every intercept packet.

Parameters​

NameType
eventany

Returns​

boolean


setID​

▸ setID(id): void

Parameters​

NameType
idstring

Returns​

void


setMetadata​

▸ setMetadata(metadata): void

Parameters​

NameType
metadataRecord<string, any>

Returns​

void


setUsername​

▸ setUsername(username): void

Parameters​

NameType
usernamestring

Returns​

void


sync​

▸ sync(): void

Returns​

void


unregister​

▸ unregister(...intercepts): Network

Parameters​

NameType
...interceptsNetIntercept[]

Returns​

Network


whenRegistryHeld​

▸ whenRegistryHeld(timeoutMs): Promise<CachedRegistry>

Resolves with the registry this client holds once it holds one (read back from storage or offered), or null after timeoutMs. Never rejects.

Parameters​

NameType
timeoutMsnumber

Returns​

Promise<CachedRegistry>