This function creates a new client socket for your game to communicate over the network.
You must define the socket type (see the list of constants below) and the function will return a unique id for that socket, which should be used in all further function calls for that socket, or a value of less than 0 if the connection fails.
TIP You can use the IP "127.0.0.1" to connect back to the same device that is running the game.
Socket Type Constant | |
---|---|
Constant | Description |
network_socket_tcp | Create a socket using TCP. |
network_socket_udp | Create a socket using UDP. |
network_socket_ws | Create a WebSocket using TCP. (NOTE: Use Async functions for connecting through WebSockets) |
network_socket_wss * | Create a secure WebSocket using TCP. |
NOTE 1 It is also possible to secure your simple WebSocket (network_socket_ws) by using the wss:// protocol in your URLs.
NOTE 2 * Secure WebSockets will not work on UWP and Xbox One when using the legacy XDK platform, however they will work on those targets when using GDK.
IMPORTANT iOS 14 and later require that you request the multicast entitlement (com.apple.developer.networking.multicast) and enable it in order to send and receive multicasts and broadcasts. To make GameMaker add this entitlement you should enable broadcast networking in the iOS Game Options / tvOS Game Options (not doing this will throw an error).
Moreover, iOS 14 does not show the consent modal dialog to the user until a packet is first sent. So the alert will not be shown if the UDP socket only receives. A way to work around this is to send an empty "dummy" packet so that the consent modal is triggered.
NOTE There is a maximum limit of 1024 sockets on Windows, macOS and Ubuntu, and 64 sockets on all other platforms.
network_create_socket(type);
Argument | Type | Description |
---|---|---|
type | Socket Type Constant | The type of socket connection to create (see the constants listed above). |
if (os_browser == browser_not_a_browser)
{
client = network_create_socket(network_socket_tcp);
network_connect( client, "192.134.0.1", 6510 );
}
else
{
client = network_create_socket(network_socket_ws);
network_connect_raw_async( client, "192.134.0.1", 6520 );
}
The above code will check whether the game is running in a browser or not and create a new TCP or Web socket before attempting to connect through that to the given IP address on the given port.