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
script: wss_destroy
script: wss_async
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 ->
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
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);
Code:
show_debug_message("Socket will now be destroyed!");
network_destroy(global.connectionSocket);
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);
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);
}
-Jaime