ENetClient
Inherits: Object
High-level ENet client singleton with automatic threading and packet encoding.
Description
ENetClient is a singleton that provides a high-level interface for connecting to game servers using the ENet library. It handles networking on a separate thread, automatically encodes/decodes packets using the same format as ENetServer, and supports multiple packet types.
The client automatically polls for network events on a background thread, ensuring the main game thread is never blocked by network operations. All signals are emitted on the main thread for safe interaction with the scene tree.
Example usage:
func _ready():
ENetClient.connected_to_server.connect(_on_connected)
ENetClient.packet_received.connect(_on_packet_received)
ENetClient.connect_to_server("127.0.0.1", 7777, 2)
func _on_connected():
print("Connected to server!")
ENetClient.send_packet({"type": "login", "username": "Player1"}, 0, true)
func _on_packet_received(packet: Variant, channel: int):
if packet is Dictionary:
match packet.get("type"):
"welcome":
print("Server says: ", packet.get("message"))
"chat":
print(packet.get("from"), ": ", packet.get("message"))
Tutorials
Methods
connect_to_server(address: String, port: int, channels: int = 2) |
|
void |
disconnect_from_server(reason: String = "") |
get_connection_status() const |
|
get_ping() const |
|
get_poll_rate() const |
|
get_server_address() const |
|
get_server_port() const |
|
get_statistic(statistic: PeerStatistic) |
|
is_connected_to_server() const |
|
void |
register_event(event_name: String, callback: Callable) |
send_node_state(node: Node, flags: int, channel: int = 0) |
|
send_packet(packet: Variant, channel: int = 0, reliable: bool = true) |
|
send_raw_packet(data: PackedByteArray, channel: int = 0, reliable: bool = true) |
|
void |
|
void |
set_poll_rate(rate_ms: int) |
trigger_event(event_name: String, payload: Dictionary, channel: int = 0, reliable: bool = true) |
|
void |
unregister_event(event_name: String) |
Signals
connected_to_server() 🔗
Emitted when successfully connected to the server.
connection_failed(reason: String) 🔗
Emitted when the connection attempt fails.
custom_event_received(event_name: String, payload: Variant, channel: int) 🔗
Emitted when a custom event packet is received and successfully routed to a registered handler.
disconnected_from_server(reason: String) 🔗
Emitted when disconnected from the server, either intentionally or due to connection loss.
packet_received(packet: Variant, channel: int) 🔗
Emitted when a packet is received from the server. The packet is automatically decoded.
raw_packet_received(data: PackedByteArray, channel: int) 🔗
Emitted when a packet is received, providing the raw byte data before decoding.
unknown_event_received(event_name: String, payload: Variant, channel: int) 🔗
Emitted when a custom event packet is received but no matching event handler is registered locally.
Enumerations
enum ConnectionStatus: 🔗
ConnectionStatus STATUS_DISCONNECTED = 0
Not connected to any server.
ConnectionStatus STATUS_CONNECTING = 1
Currently attempting to connect to a server.
ConnectionStatus STATUS_CONNECTED = 2
Successfully connected to a server.
Method Descriptions
Error connect_to_server(address: String, port: int, channels: int = 2) 🔗
Initiates a connection to the server at the specified address and port. The connection process is asynchronous; listen for the connected_to_server or connection_failed signals.
Returns @GlobalScope.OK if the connection attempt started successfully, or an error code if it failed to start.
void disconnect_from_server(reason: String = "") 🔗
Disconnects from the server with an optional reason.
ConnectionStatus get_connection_status() const 🔗
Returns the current connection status.
Returns the current round-trip time (ping) to the server in milliseconds.
Returns the current polling rate in milliseconds.
String get_server_address() const 🔗
Returns the address of the server we're connected to (or attempting to connect to).
Returns the port of the server we're connected to (or attempting to connect to).
float get_statistic(statistic: PeerStatistic) 🔗
Returns the specified network statistic. See PeerStatistic.
bool has_event(event_name: String) const 🔗
Returns true if a custom event handler is registered for the specified event name.
bool is_connected_to_server() const 🔗
Returns true if currently connected to a server.
void register_event(event_name: String, callback: Callable) 🔗
Registers a custom event callback. The callable will be executed whenever a custom event packet with the matching event_name is received.
func _ready():
ENetClient.register_event("server_status", _on_status)
func _on_status(payload: Dictionary, channel: int):
print("Server update: ", payload)
Error send_node_state(node: Node, flags: int, channel: int = 0) 🔗
Sends a serialized node state to the server. The flags parameter controls what data is sent. See NodeSyncFlags.
Error send_packet(packet: Variant, channel: int = 0, reliable: bool = true) 🔗
Sends a packet to the server. The packet can be of any type (Dictionary, String, int, etc.) and will be automatically encoded using the same format as ENetServer.
ENetClient.send_packet({"action": "jump", "power": 1.5}, 0, true)
Error send_raw_packet(data: PackedByteArray, channel: int = 0, reliable: bool = true) 🔗
Sends raw byte data directly to the server without ENet packet type headers or serialization.
var byte_arr = PackedByteArray([0x01, 0x02, 0x03, 0x04])
ENetClient.send_raw_packet(byte_arr, 0, true)
void set_compression_mode(mode: CompressionMode) 🔗
Sets the compression mode for network packets. Must match the server's compression mode. See CompressionMode.
void set_poll_rate(rate_ms: int) 🔗
Sets how often (in milliseconds) the polling thread checks for network events. Lower values mean lower latency but higher CPU usage. Default is 10ms.
Error trigger_event(event_name: String, payload: Dictionary, channel: int = 0, reliable: bool = true) 🔗
Triggers a custom event on the server side by sending a specially formatted packet containing the event_name and payload.
void unregister_event(event_name: String) 🔗
Unregisters a previously registered custom event handler.