gale.net.client module

This file contains the implementation of the class Client, the connecting side of a gale.net connection.

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.net.client.Client(serialize=<function json_serialize>, deserialize=<function json_deserialize>, timeout=5.0)[source]

Bases: object

The connecting side of a gale.net game: connects to a single Server over a non-blocking UDP socket, and lets the game send messages to it and react to the ones it sends back.

Usage example:

client = Client() client.on_connect(lambda: print(“connected”)) client.on_message(“snapshot”, lambda payload: …) client.connect(“127.0.0.1”, 9000)

# In the game loop: client.update(dt) client.send(“input”, {“move”: “left”})

Parameters:
  • serialize (Callable[[str, Dict[str, Any]], bytes])

  • deserialize (Callable[[bytes], Tuple[str, Dict[str, Any]]])

  • timeout (float)

on_connect(callback)[source]
Parameters:

callback (Callable[[], None]) – Called with no arguments once the connection is established.

Return type:

None

on_connect_failed(callback)[source]
Parameters:

callback (Callable[[str], None]) – Called with a reason string if connect() times out without a reply.

Return type:

None

on_disconnect(callback)[source]
Parameters:

callback (Callable[[str], None]) – Called with a reason string when a previously established connection ends, explicitly or by timing out.

Return type:

None

on_message(message_type, callback)[source]
Parameters:
  • message_type (str) – The message type to react to.

  • callback (Callable[[Dict[str, Any]], None]) – Called with the payload every time a message of this type is received.

Return type:

None

connect(host, port, timeout=5.0)[source]

Start connecting to a Server. Non-blocking: the outcome is reported later, through update(), via the on_connect/ on_connect_failed callbacks.

Parameters:
  • host (str) – The server’s address.

  • port (int) – The server’s port.

  • timeout (float) – How long to keep retrying before giving up, in seconds. The default value is 5.0.

Return type:

None

get_rtt()[source]
Returns:

The smoothed round-trip time to the server, in seconds, or None if not connected.

Return type:

float | None

send(message_type, payload, channel=0)[source]
Parameters:
  • message_type (str) – The message type.

  • payload (Dict[str, Any]) – The message data.

  • channel (int) – One of the Channel constants. The default value is Channel.UNRELIABLE.

Return type:

None

disconnect()[source]

End the current connection, if any, telling the server so it does not have to wait for a timeout to notice. Does not notify this client’s own on_disconnect (that is reserved for connections that end unexpectedly).

Return type:

None

close()[source]

Close the underlying socket. The client is no longer usable afterwards.

Return type:

None

update(dt)[source]

Poll the network and drive the connection state: attempt/retry connecting, receive and dispatch messages, retransmit unacked reliable packets, send heartbeats, and detect a timed-out server.

Parameters:

dt (float) – Time elapsed, in seconds, since the last call. Currently unused (timing is wall-clock based), accepted for consistency with the rest of gale.

Return type:

None