• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker A simple networking issue is driving me crazy! I'd love some help.

J

JB4GDI

Guest
Hey everyone!

I have been struggling for hours trying to get a simple websocket connection to work and it's driving me crazy. Any help would be immensely appreciated.

There is a WSS connection at https://www.websocket.org/echo.html and it literally just takes whatever you send to it, and spits it right back out at you.

I can connect to it, and I can send stuff to it, but I can't see anything that comes back. There's a breakpoint in my Async - Networking event but it never fires.

3 Scripts and 1 object. If you want to just download the project, it's at http://www.boardgamefightclub.com/WSSTest.yyz. Otherwise:

script: wss_connect
Code:
    // We are just connecting to an echo chamber
    global.wssURL = "echo.websocket.org";
   
   
    // Make a new TCP connection
    global.connectionSocket = network_create_socket(network_socket_tcp);   
   
   
    // If something went really, really wrong, destroy the connection
    if (global.connectionSocket < 0) {   
        show_debug_message("Socket creation failed!");   
        network_destroy(global.connectionSocket);
        exit;
    } else {
        show_debug_message("Socket created!  Proceeding");
    }
   
   
    // Get the IP address of the chat
    var wssIP = network_resolve(global.wssURL);
    show_debug_message("Connecting to " + global.wssURL);
   
   
    // Create a connection to the echo chamber!
    var rawStream = network_connect_raw(global.connectionSocket, string(wssIP), 443);
    if (rawStream < 0) {
        network_destroy(global.connectionSocket);
        global.connectionSocket = -1;   
    show_debug_message("Failed to connect to " + string(wssIP) + " on port 443");
    exit;
    } else {
        show_debug_message("Successfully connected to " + string(wssIP) + " on port 443");   
    }
   
   
    // Send a payload to test!
    var testMessage = "Send this test message";
   
    var send_buff1 = buffer_create(128, buffer_fixed, 1);
    buffer_seek(send_buff1, buffer_seek_start, 0);
    buffer_write(send_buff1, buffer_string, testMessage + string(chr(13) + chr(10)));
    // IMPORTANT:  We just moved the position in the buffer by writing so we have to seek 
    // it back to the beginning again.
    buffer_seek(send_buff1, buffer_seek_start, 0);
    var result = network_send_raw(global.connectionSocket, send_buff1, buffer_get_size(send_buff1));   
    show_debug_message("Sent message: " + buffer_read(send_buff1, buffer_string));
    show_debug_message(string(result));
    buffer_delete(send_buff1);
script: wss_destroy
Code:
    show_debug_message("Socket will now be destroyed!");
    network_destroy(global.connectionSocket);
script: wss_async
Code:
    var net_buff = ds_map_find_value(async_load,"buffer");
    var net_size = buffer_get_size(net_buff);
    buffer_seek(net_buff,buffer_seek_start,0);
   
    var data_list  = ds_list_create();
   
    // received all lines of text and add to list to be evaluated
    while(buffer_tell(net_buff) < buffer_get_size(net_buff)) {
        var str = buffer_read(net_buff,buffer_string);
        ds_list_add(data_list,str);
    }
   
    // loop through received data
    for(var i=0; i<ds_list_size(data_list); i++;) {
       
        // next string of data
        var str = ds_list_find_value(data_list,i);
       
        show_debug_message(str);
   
    }

    ds_list_destroy(data_list);
1 object that goes in a room: obj_wss_connect

Create event -> wss_connect();
Destroy event -> wss_destroy();
Game end event -> wss_destroy();
Async - Networking event -> wss_async();

Step event ->
Code:
    if (keyboard_check_pressed(ord("C"))) {   
       
        var testMessage = "Send this second test message";
       
        // This is literally the exact same code that we call when we connect
        var send_buff1 = buffer_create(128, buffer_fixed, 1);
        buffer_seek(send_buff1, buffer_seek_start, 0);
        buffer_write(send_buff1, buffer_string, testMessage + string(chr(13) + chr(10)));
        buffer_seek(send_buff1, buffer_seek_start, 0);
        var result = network_send_raw(global.connectionSocket, send_buff1, buffer_get_size(send_buff1));
        show_debug_message("Sent message: " + buffer_read(send_buff1, buffer_string));
        show_debug_message(string(result));
        buffer_delete(send_buff1);
    }
It's killing me that I've done similar things before but the WSS connection won't give me anything back. If you can help, I'm open to trying anything. Thanks!

-Jaime
 

FrostyCat

Redemption Seeker
That's because your attempt to talk to it was improper. This is a wss:// endpoint, the extra "s" tells you not to send it plaintext.

Unless you're willing to implement TLS/SSL in GML as well, your attempt to do WebSockets in pure GML is a fool's errand. You'd have better luck with writing an extension to integrate with existing WebSockets libraries.
 
J

JB4GDI

Guest
Geez, that is totally it. Looks like this is my only option at the moment then, but the Marketplace seems like it's not working?

Thank you so much for figuring it out FrostyCat!
 
Top