Pixel-Composer/scripts/node_websocket_receiver/node_websocket_receiver.gml

141 lines
3.7 KiB
Text
Raw Normal View History

2023-06-04 12:38:40 +02:00
function Node_Websocket_Receiver(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Websocket Receiver";
2024-03-28 14:18:02 +01:00
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Int("Port", self, 22400));
2023-06-04 12:38:40 +02:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Bool("Active", self, true));
2023-06-04 12:38:40 +02:00
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Enum_Button("Mode", self, 1, [ "Client", "Server" ]));
2024-05-18 13:46:01 +02:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Text("Url", self, ""));
2024-05-18 13:46:01 +02:00
2024-08-08 06:57:51 +02:00
outputs[0] = nodeValue_Output("Data", self, VALUE_TYPE.struct, {});
2023-06-04 12:38:40 +02:00
2024-08-08 06:57:51 +02:00
outputs[1] = nodeValue_Output("Receive data", self, VALUE_TYPE.trigger, false);
2023-06-04 12:38:40 +02:00
2024-05-18 13:46:01 +02:00
input_display_list = [ 1, 2,
["Connection", false], 0, 3,
];
2023-06-04 12:38:40 +02:00
connected_device = 0;
2024-05-18 13:46:01 +02:00
network_trigger = 0;
2023-07-05 15:09:52 +02:00
port = 0;
2024-05-18 13:46:01 +02:00
mode = 0;
url = "";
2023-07-05 15:09:52 +02:00
socket = noone;
2024-05-18 13:46:01 +02:00
client = noone;
2023-06-04 12:38:40 +02:00
2024-05-18 13:46:01 +02:00
function setPort() { #region
var _port = getInputData(0);
var _mode = getInputData(2);
var _url = getInputData(3);
if(_port == port && _mode == mode && _url == url) return;
port = _port;
mode = _mode;
url = _url;
2023-06-04 12:38:40 +02:00
if(ds_map_exists(PORT_MAP, port))
array_remove(PORT_MAP[? port], self);
if(!ds_map_exists(PORT_MAP, port))
PORT_MAP[? port] = [];
array_push(PORT_MAP[? port], self);
2024-05-18 13:46:01 +02:00
if(socket != noone)
network_destroy(socket);
if(mode == 0) {
client = network_create_socket(network_socket_ws);
network_connect_raw(client, url, port);
} else if(mode == 1) {
socket = network_create_server_raw(network_socket_ws, port, 16);
if(socket) NETWORK_SERVERS[? port] = socket;
2024-05-18 13:46:01 +02:00
}
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("Refresh Server"), [ THEME.refresh_icon, 1, COLORS._main_value_positive ], function() { setPort(); });
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;
var _active = getInputData(1);
2023-06-04 12:38:40 +02:00
if(!_active) return;
var type = async_load[? "type"];
switch(type) {
case network_type_connect :
2024-06-26 06:38:57 +02:00
var _txt = $"Websocket server: Client connected at port {port} on node {display_name}";
logNode(_txt); noti_status(_txt);
2023-06-04 12:38:40 +02:00
connected_device++;
break;
2024-05-18 13:46:01 +02:00
2023-06-04 12:38:40 +02:00
case network_type_disconnect :
2024-06-26 06:38:57 +02:00
var _txt = $"Websocket server: Client disconnected at port {port} on node {display_name}";
logNode(_txt); noti_status(_txt);
2023-06-04 12:38:40 +02:00
connected_device--;
break;
2024-05-18 13:46:01 +02:00
2023-06-04 12:38:40 +02:00
case network_type_data :
2024-05-18 13:46:01 +02:00
var _buffer = async_load[? "buffer"];
var _socket = async_load[? "id"];
var data = buffer_get_string(_buffer);
2023-06-04 12:38:40 +02:00
2023-06-24 22:12:35 +02:00
var _data = json_try_parse(data, noone);
2024-05-18 13:46:01 +02:00
if(_data == noone) _data = { rawData: new Buffer(_buffer) }
else buffer_delete(_buffer);
2023-06-24 22:12:35 +02:00
2024-08-08 06:57:51 +02:00
outputs[0].setValue(_data);
2023-06-04 12:38:40 +02:00
network_trigger = true;
break;
}
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
2024-05-18 13:46:01 +02:00
var _mode = getInputData(2);
2024-08-08 06:57:51 +02:00
inputs[3].setVisible(_mode == 0);
2024-05-18 13:46:01 +02:00
2023-06-13 14:42:06 +02:00
if(network_trigger == 1) {
2024-08-08 06:57:51 +02:00
outputs[1].setValue(1);
2023-06-13 14:42:06 +02:00
network_trigger = -1;
} else if(network_trigger == -1) {
2024-08-08 06:57:51 +02:00
outputs[1].setValue(0);
2023-06-13 14:42:06 +02:00
network_trigger = 0;
}
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
2023-06-05 18:27:53 +02:00
if(CLONING) return;
2024-05-18 13:46:01 +02:00
setPort();
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 onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
var _active = getInputData(1);
2023-06-04 12:38:40 +02:00
var bbox = drawGetBbox(xx, yy, _s);
var network = ds_map_try_get(NETWORK_SERVERS, port, noone);
var cc = CDEF.red, aa = 1;
if(network >= 0) cc = CDEF.lime;
if(!_active) aa = 0.5;
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_valign(fa_bottom)
draw_text_add(bbox.xc, bbox.y1, $"{connected_device} " + __txt("Connected"), _ts);
2023-06-04 12:38:40 +02:00
draw_set_alpha(1);
draw_sprite_fit(THEME.node_websocket_receive, 0, bbox.xc, (_y0 + _y1) / 2, bbox.w, _y1 - _y0, cc, aa);
2023-11-22 05:17:13 +01:00
} #endregion
2023-06-04 12:38:40 +02:00
}