Game Maker WebSockets Extension

This project is discontinued as Game Maker Studio 2 now has Web Sockets integrated to its internal system. Please use GMS2’s Web Sockets.

WebSockets Extension is a lightweight websocket extension that allows you to send and receive data over WebSocket protocol.

  • Lightweight. Doesn’t use any external files and is made with Javascript Websockets. Can be used easily with Facebook Instant Games.
  • Can connect multiple servers : In case if you need to connect multiple servers. Can send data to all of the connections, and receive from all of them.
  • Clean code (Passes data to Game Maker via Social Async Event)

DOCUMENTATION

web_socket_debug_mode(True/False debugMode);
Description
: Enables or disables the debugMode. You can see what’s happening in the background on Browser Console.
Returns : N/A

web_socket_is_supported();
Description
: Checks if WebSockets are supported in the browser. Most modern browsers do.
Returns : true or false

web_socket_open(Real connectionNumber, String serverAddress, dsList protocol, True/False JSON Optimize);
Description
: Opens a socket to the designated address. connectionNumber indicates the ID number of the connection. You must set it as 0, 1, 2 etc.. in order. serverAddress is the webserver’s address and dsList is a ds_list contains protocols you wish to add to your connection(You can set it as -1 if you don’t want to add any protocols to it). If you plan to receive JSON type messages from this socket, set JSON Optimize as true, as it will force the extension to prepare itself for that scenario. If you just send a simple string or any other format, you must set it as false.

Returns : N/A
Callbacks : A Social Async Event
“id” = It must be equal to async_websocket (Constant)
“connection_number” = Indicates which connection the package comes from.
event_type” = For why we received the package. It returns;

  • socket_event_connected (Constant) : When connected to the server.
  • socket_event_closed (Constant) : When the connection is closed.
  • socket_event_error (Constant) : When there’s an error.
  • socket_event_message (Constant) : When we receive a data package.

If the “event_type” is socket_event_message, some extra keys are added to the Social Async Event.
“json_format” : true or false. This means that the connection was opened with JSON Optimize format, and you must parse the message data with json_decode() function to convert it to a ds_map.
“data” = The message data. You can convert it to a ds_map by using json_decode, or if you set JSON Optimize argument as false, you can use it as it is.

Connecting to the server

web_socket_open(0,"wss://echo.websocket.org/",-1,true);

Receiving Data from Server

if (async_load[? "id"]==async_websocket) //The package belongs to us.
{
	
	var ev_type, server_no;
	ev_type = async_load[? "event_type"];
	server_no = async_load[? "server_number"];

	switch(ev_type)
	{
		case socket_event_connected: //If we are connected
		//Good
		break;
		
		case socket_event_closed:
		//Connection is closed!
		break;
		
		case socket_event_error: 
		//There's.. erm... an error!!!
		break;
		
		case socket_event_message:
		show_message_async(data);
		
		var data = async_load[? "data"];
		
		var map = json_decode(data);
		
		var myMessage = map[? "username"];
		var meLevel = map[? "level"];
		break;
		
		
	}
	
}

web_socket_send(Real connectionNumber, String Data);
Description
: Sends a data to the server. connectionNumber is the connection we set before. You can send multiple messages to multiple servers. For sending JSON, you can convert a ds_map to a json string with json_decode
Returns : N/A

var map = ds_map_create();
map[? "myFavouriteSinger"] = "Bryan Adams";
map[? "whoLetTheDogsOut"] = "Who Who? Who WHo??";
map[? "level"] = 25;
var jsonString = json_encode(map);
web_socket_send(0, jsonString);
ds_map_destroy(map); //Destroy the map, because we're done with it.

web_socket_close(Real connectionNumber);
Description : Closes the socket, therefore the connection to the server.
Returns : N/A
Callbacks : A Social Async Event
“id” = async_websocket (Constant)
“event_type” = socket_event_closed (Constant)

GET THE EXTENSION

Open Source

Share

Marty

Truely speaking, I don't know what am I doing most of the time.

Leave a Reply