Pixel-Composer/scripts/node_websocket_sender/node_websocket_sender.gml

161 lines
4.6 KiB
Plaintext
Raw Normal View History

2023-06-04 12:38:40 +02:00
function Node_Websocket_Sender(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Websocket Sender";
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Int("Port", self, 22800));
2023-06-04 12:38:40 +02:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Enum_Scroll("Data type", self, 0, [ "Struct", "Surface", "File", "Buffer" ]));
2023-06-04 12:38:40 +02:00
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Struct("Struct", self, {}));
2023-06-04 12:38:40 +02:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Surface("Surface", self));
2023-06-04 12:38:40 +02:00
2024-08-18 09:13:41 +02:00
newInput(4, nodeValue_Path("Path", self, ""))
2023-06-04 12:38:40 +02:00
.setDisplay(VALUE_DISPLAY.path_load)
.nonValidate();
2024-08-18 06:16:20 +02:00
newInput(5, nodeValue_Text("Target", self, "127.0.0.1"));
2023-06-24 22:12:35 +02:00
2024-08-18 06:16:20 +02:00
newInput(6, nodeValue_Buffer("Buffer", self, noone));
2023-06-24 22:12:35 +02:00
input_display_list = [ 5, 0, 1, 2, 3, 4, 6 ];
2023-06-04 12:38:40 +02:00
2023-11-22 05:17:13 +01:00
port = 0;
url = "";
2023-06-04 12:38:40 +02:00
connected = false;
2023-11-22 05:17:13 +01:00
callbackMap = {};
attributes.network_timeout = 1000;
array_push(attributeEditors, "Network");
array_push(attributeEditors, [ "Connection timeout", function() { return attributes.network_timeout; },
new textBox(TEXTBOX_INPUT.number, function(val) {
attributes.network_timeout = val;
network_set_config(network_config_connect_timeout, val);
}) ]);
static connectTo = function(newPort, newUrl, params) { #region
2024-06-26 06:38:57 +02:00
logNode($"Connecting to {newUrl}:{newPort}");
2023-11-22 05:17:13 +01:00
2023-06-04 12:38:40 +02:00
if(ds_map_exists(PORT_MAP, port))
array_remove(PORT_MAP[? port], self);
port = newPort;
url = newUrl;
2023-11-22 05:17:13 +01:00
if(ds_map_exists(NETWORK_CLIENTS, port))
network_destroy(NETWORK_CLIENTS[? port]);
2023-06-04 12:38:40 +02:00
2023-11-22 05:17:13 +01:00
var socket = network_create_socket(network_socket_ws);
if(socket < 0) {
2024-06-26 06:38:57 +02:00
var _txt = "Websocket sender: Fail to create new socket.";
logNode(_txt); noti_warning(_txt);
2023-06-04 12:38:40 +02:00
return;
2023-11-22 05:17:13 +01:00
}
2023-07-05 15:09:52 +02:00
2023-11-22 05:17:13 +01:00
var _conId = network_connect_raw_async(socket, url, port);
PORT_MAP[? _conId] = self;
callbackMap[$ _conId] = params;
NETWORK_CLIENTS[? _conId] = socket;
2023-07-05 15:09:52 +02:00
2024-06-26 06:38:57 +02:00
logNode($"Connected to {newUrl}:{newPort}");
2023-11-22 05:17:13 +01:00
} #endregion
2023-06-04 12:38:40 +02:00
2024-05-18 13:46:01 +02:00
setInspector(1, __txt("Resend"), [ THEME.refresh_icon, 1, COLORS._main_value_positive ], function() { triggerRender(); });
2023-11-22 05:17:13 +01:00
static sendCall = function(ID, params) {
var network = ds_map_try_get(NETWORK_CLIENTS, ID, noone);
if(network < 0) {
2024-06-26 06:38:57 +02:00
var _txt = "Websocket sender: No client.";
logNode(_txt); noti_warning(_txt);
2023-11-22 05:17:13 +01:00
return;
}
var content = params.content;
2024-06-26 06:38:57 +02:00
var res = network_send_raw(network, content, buffer_get_size(content));
if(res < 0) {
var _txt = "Websocket sender: Send error.";
logNode(_txt); noti_warning(_txt);
}
2023-06-04 12:38:40 +02:00
}
2023-11-22 05:17:13 +01:00
static asyncPackets = function(_async_load) { #region
2023-06-17 18:59:20 +02:00
if(!active) return;
2023-11-22 05:17:13 +01:00
var aid = async_load[? "id"];
2023-06-04 12:38:40 +02:00
var type = async_load[? "type"];
2023-11-22 05:17:13 +01:00
if(type == network_type_non_blocking_connect) {
2024-06-26 06:38:57 +02:00
var _txt = $"Websocket sender: Connected at port {port} on node {display_name}";
logNode(_txt); noti_status(_txt);
2023-11-22 05:17:13 +01:00
connected = true;
var callBack = callbackMap[$ aid];
sendCall(aid, callBack);
2023-06-04 12:38:40 +02:00
}
2023-11-22 05:17:13 +01:00
} #endregion
2023-06-04 12:38:40 +02:00
2023-11-22 05:17:13 +01:00
static step = function() { #region
var _type = getInputData(1);
2023-06-04 12:38:40 +02:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(_type == 0, _type == 0);
inputs[3].setVisible(_type == 1, _type == 1);
inputs[4].setVisible(_type == 2, _type == 2);
inputs[6].setVisible(_type == 3, _type == 3);
2023-11-22 05:17:13 +01:00
} #endregion
2023-06-04 12:38:40 +02:00
2023-11-22 05:17:13 +01:00
static update = function(frame = CURRENT_FRAME) { #region
var _port = getInputData(0);
var _target = getInputData(5);
2023-06-04 12:38:40 +02:00
var _type = getInputData(1);
2023-06-24 22:12:35 +02:00
var _buff, res;
2023-11-22 05:17:13 +01:00
var params = {};
2023-06-04 12:38:40 +02:00
switch(_type) {
case 0 :
var _stru = getInputData(2);
2023-06-04 12:38:40 +02:00
var _str = json_stringify(_stru);
2023-06-24 22:12:35 +02:00
_buff = buffer_from_string(_str);
2023-06-04 12:38:40 +02:00
break;
case 1 :
var _surf = getInputData(3);
2023-06-04 12:38:40 +02:00
if(!is_surface(_surf)) return;
2023-06-24 22:12:35 +02:00
_buff = buffer_from_surface(_surf);
2023-06-04 12:38:40 +02:00
break;
case 2 :
var _path = getInputData(4);
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(_path)) return;
2023-06-24 22:12:35 +02:00
_buff = buffer_from_file(_path);
break;
case 3 :
_buff = getInputData(6);
2023-06-24 22:12:35 +02:00
if(!buffer_exists(_buff)) return;
2023-06-04 12:38:40 +02:00
break;
}
2023-11-22 05:17:13 +01:00
params.content = _buff;
connectTo(_port, _target, params);
} #endregion
2023-06-04 12:38:40 +02:00
2023-11-22 05:17:13 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
2023-06-04 12:38:40 +02:00
var bbox = drawGetBbox(xx, yy, _s);
var network = ds_map_try_get(NETWORK_CLIENTS, port, noone);
2023-11-22 05:17:13 +01:00
var cc = CDEF.lime, aa = 1;
2023-06-04 12:38:40 +02:00
var _y0 = bbox.y0 + ui(16);
var _y1 = bbox.y1 - ui(16);
var _ts = _s * 0.75;
draw_set_text(f_code, fa_center, fa_top, COLORS._main_text);
draw_set_alpha(0.75);
draw_text_add(bbox.xc, bbox.y0, $"Port {port}", _ts);
draw_set_alpha(1);
draw_sprite_fit(THEME.node_websocket_send, 0, bbox.xc, (_y0 + _y1) / 2, bbox.w, _y1 - _y0, cc, aa);
2023-11-22 05:17:13 +01:00
} #endregion
static doApplyDeserialize = function() { #region
if(struct_has(attributes, "network_timeout")) network_set_config(network_config_connect_timeout, attributes.network_timeout);
} #endregion
2023-06-04 12:38:40 +02:00
}