Skip to content

API Documentation

Ignas Kavaliauskas edited this page Aug 29, 2019 · 10 revisions

sio_connect()

Connects to the window.location address. Call this function before continuing using this extension.

sio_connect_by_url(url:string)

Same as sio_connect() but with the difference that you can connect to a different url.

sio_disconnect()

Closes the current connection.

sio_reconnect()

In case you've disconnected for whatever reason, you can use this function to reconnect to the server. (It will use the provided address from sio_connect() or sio_connect_by_url(url:string) (If connection is lost, socket.io will try re-connect by itself) You don't need to implement this feature by yourself.

sio_addEvent(eventName:string)

Creates a new event, but you have to create a callback to your client in order to access the received data. ATTENTION you have to follow this naming scheme: gmcallback_sio_on_**eventName**. Where **eventName** is replaced by the name you have specified above as an argument. Event names are case sensitive!.

Example:

# create event
sio_addEvent("create_player");
create a new script called: "gmcallback_sio_on_create_player"

The callback script contains one argument argument0, which is the packet data received from the server. If the server sends a JSON-object to the client, the client turns it automatically into a JSON-string. Otherwise it just passes the string right away.

In this example our argument0 is a JSON-string. To use the data, you've to decode the JSON-string to DSMap.

Example:

In script gmcallback_sio_on_create_player
// Decode the received JSON string to DSMap
var data = json_decode(argument[0]);

// Access our data
var username = data[? "username"];
var playerID = data[? "id"];
var health = real(data[? "health"]);
..

sio_emit(eventName:string, data:string)

Send your data to the server. eventName is case sensitive! data can be either a string or JSON-string. In order to send a packet, follow the example below. The data we send is a JSON-string.

Example:

In script sio_emit_create_player
var eventName = "create_player";
var username = "John Doe";

// This is our packet
var data = ds_map_create();
  data[? "username"] = username;
  data[? "x"] = x;
  data[? "y"] = y;
  sio_emit(eventName, json_encode(data));
ds_map_destroy(data);

sio_get_connection_status() -> boolean

If the game client is connected to the server it will return true, otherwise false