Skip to main content

A network connector to the Voxelize backend. Establishes a WebSocket connection to the backend server and handles the Protocol Buffer encoding and decoding.

Example

const network = new VOXELIZE.Network();

network
.connect("ws://localhost:5000")
.then(() => {
network.join("my-world").then(() => {
console.log("Joined world!");
});
});

Constructors

constructor

new Network(options?): Network

Create a new network instance.

Parameters

NameType
optionsPartial<NetworkOptions>

Returns

Network

Properties

clientInfo

clientInfo: Object

Information about the client that is sent to the server on connection. Initialize the username through setUsername and the id through setID. If nothing is set, then the information will be generated by the server and sent back to this client.

This is also the information passed into NetIntercept callbacks.

Type declaration

NameTypeDescription
idstringThe unique ID of the client. This can be set by setID BEFORE connecting to the server. If this is set before connection, then the ID will be used and the server will not generate a new ID for this client.
usernamestringThe username of the client. This can be set by setUsername BEFORE connecting to the server. Setting this username after connecting to the server will not change anything.

connected

connected: boolean = false

Whether or not the network connection is established.


intercepts

intercepts: NetIntercept[] = []

A list of network event interceptors that are called when a network event is received. You can add interceptors by calling register and remove them by calling unregister.


joined

joined: boolean = false

Whether or not the client has joined a specific world on the server.


onConnect

onConnect: () => void

Type declaration

▸ (): void

A custom event listener that is called when this network instance is connected to a server.

Returns

void


onDisconnect

onDisconnect: () => void

Type declaration

▸ (): void

A custom event listener that is called when this network instance is disconnected from a server.

Returns

void


onJoin

onJoin: (world: string) => void

Type declaration

▸ (world): void

A custom event listener that is called when this network instance has joined a world.

Parameters
NameType
worldstring
Returns

void


onLeave

onLeave: (world: string) => void

Type declaration

▸ (world): void

A custom event listener that is called when this network instance has left a world.

Parameters
NameType
worldstring
Returns

void


options

options: NetworkOptions


socket

socket: URL

A native URL instance constructed with network.options.serverURL, representing a WebSocket connection URL to the server.


url

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

A domurl Url instance constructed with network.options.serverURL, representing a HTTP connection URL to the server.


world

world: string

The name of the world that the client is connected to. This is only set after the connection is established.


ws

ws: ProtocolWS

The inner WebSocket client for Voxelize, with support for protocol buffers.

Accessors

concurrentWorkers

get concurrentWorkers(): number

The number of active web workers decoding network packets.

Returns

number


packetQueueLength

get packetQueueLength(): number

The number of network packets waiting to be decoded.

Returns

number

Methods

action

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

Send an ACTION type message to the server.

Parameters

NameTypeDescription
typestringThe type of action to perform.
data?anyThe specific data attached to this action.

Returns

Promise<void>


connect

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

Connect to a Voxelize server. Remember to set username and ID before connection if you want to specify them manually. Otherwise ID is generated by the server, and username would be "Guest XXXXX" where XXXXX is a random 5-digit number.

Parameters

NameTypeDescription
serverURLstringThe URL to the Voxelize server.
optionsNetworkConnectionOptionsParameters to customize the connection to a Voxelize server.

Returns

Promise<Network>

A promise that resolves when the client has connected to the server.


disconnect

disconnect(): void

Disconnect the client from the server.

Returns

void


flush

flush(): void

Gathers all the network packets from the network intercepts and sends them to the server. This method should be called at the end of each client-side game tick.

Returns

void


join

join(world): Promise<Network>

Join a world on the server.

Parameters

NameTypeDescription
worldstringThe name of the world to join.

Returns

Promise<Network>

A promise that resolves when the client has joined the world.


leave

leave(): void

Leave the current world. If the client is not in a world, this method does nothing.

Returns

void

A promise that resolves when the client has left the world.


register

register(...intercepts): this

Register a network intercept to the network. This is used so that one can define the reaction to the network packets received. For instance, one can define a network intercept to handle the EVENT type messages and perform something based on the

Parameters

NameTypeDescription
...interceptsNetIntercept[]One or more intercepts to add to the network.

Returns

this

The network instance itself for chaining.


send

send(event): void

Send a raw network packet to the server. Must be a valid network packet, or else the server may crash.

Parameters

NameTypeDescription
eventanyThe event packet to send to the server.

Returns

void


setID

setID(id): void

Set the client's ID. This needs to be called before the network has connected to the server, otherwise the client will be assigned a server-generated ID.

Parameters

NameTypeDescription
idstringThe ID of the client that is used to identify the client on server connection.

Returns

void


setUsername

setUsername(username): void

Set the client's username. This needs to be called before the network has connected to the server, otherwise the client will be assigned a Guest XXXXX username.

Parameters

NameTypeDescription
usernamestringThe username of the client that is used to identify the client on server connection.

Returns

void


sync

sync(): void

Returns

void


unregister

unregister(...intercepts): this

Unregister a network intercept from the network.

Parameters

NameTypeDescription
...interceptsNetIntercept[]One or more intercepts to remove from the network.

Returns

this

The network instance itself for chaining.