Network
API for network-related functions.
All network messages are identified by a 64-bit hash of their name. This hash is automatically computed when using Network.send and Network.receive, so you don't need to worry about it unless you're handling the low-level Network.message event directly.
Every network message is limited to a maximum payload size of 1 megabyte (1,048,576 bytes). All data is sent on the reliable channel.
Functions
Network.send
Sends a buffer of data to a specified player as a specific network message.
Network.send(payload: buffer, messageName: string, player: Player)
Parameters
| Name | Type | Description |
|---|---|---|
| payload | buffer | The buffer of data to send. |
| messageName | string | The name of the network message to send. |
| player | Player | The player to send the data to. |
Network.send
Sends a buffer of data to the server as a specific network message.
Network.send(payload: buffer, messageName: string)
Parameters
| Name | Type | Description |
|---|---|---|
| payload | buffer | The buffer of data to send. |
| messageName | string | The name of the network message to send. |
Network.receive
Registers a callback to be called when a specific network message is received from a player.
Network.receive(messageName: string, callback: fun(player: Player, payload: buffer))
Parameters
| Name | Type | Description |
|---|---|---|
| messageName | string | The name of the network message to listen for. |
| callback | fun(player: Player, payload: buffer) | The callback to be called when the message is received. |
Network.receive
Registers a callback to be called when a specific network message is received from the server.
Network.receive(messageName: string, callback: fun(payload: buffer))
Parameters
| Name | Type | Description |
|---|---|---|
| messageName | string | The name of the network message to listen for. |
| callback | fun(payload: buffer) | The callback to be called when the message is received. |
Network.getMessageNameHash
Gets the hash of a network message name.
Network.getMessageNameHash(messageName: string): string
Parameters
| Name | Type | Description |
|---|---|---|
| messageName | string | The name of the network message. |
Returns
| Name | Type | Description |
|---|---|---|
| hash | string | The 64-bit hash of the network message name as a string. |
Network.addEventListener
Adds an event listener. A list of events can be found here.
Network.addEventListener(eventType: string, callback: function): function
Parameters
| Name | Type | Description |
|---|---|---|
| eventType | string | The event to listen for. |
| callback | function | The callback to use. |
Returns
| Name | Type | Description |
|---|---|---|
| callback | function | The callback that was registered. |
Network.removeEventListener
Removes an event listener.
Network.removeEventListener(eventType: string, callback: function)
Parameters
| Name | Type | Description |
|---|---|---|
| eventType | string | The event to remove the listener from. |
| callback | function | The callback to remove. |
Events
"message"
Fired when a network message is received from the server.
- Network messages are identified by a hash, not by name.
- Use
Network.receiveto register callbacks for specific messages.
Network.addEventListener("message", fn(messageNameHash, payload) end)
Parameters
| Name | Type | Description |
|---|---|---|
| messageNameHash | string | The 64-bit hash of the received network message as a string. |
| payload | buffer | The buffer of data received. |
"message"
Fired when a network message is received from a player.
- Network messages are identified by a hash, not by name.
- Use
Network.receiveto register callbacks for specific messages.
Network.addEventListener("message", fn(player, messageNameHash, payload) end)
Parameters
| Name | Type | Description |
|---|---|---|
| player | Player | The player who sent the message. |
| messageNameHash | string | The 64-bit hash of the received network message as a string. |
| payload | buffer | The buffer of data received. |