2023-06-04 12:38:40 +02:00
|
|
|
function Node_Websocket_Receiver(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
|
|
|
|
name = "Websocket Receiver";
|
|
|
|
|
|
|
|
w = 128;
|
|
|
|
h = 128;
|
|
|
|
min_h = h;
|
|
|
|
|
|
|
|
inputs[| 0] = nodeValue("Port", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 22400);
|
|
|
|
|
|
|
|
inputs[| 1] = nodeValue("Active", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
|
|
|
|
|
|
|
|
outputs[| 0] = nodeValue("Data", self, JUNCTION_CONNECT.output, VALUE_TYPE.struct, {});
|
|
|
|
|
|
|
|
outputs[| 1] = nodeValue("Receive data", self, JUNCTION_CONNECT.output, VALUE_TYPE.trigger, 0);
|
|
|
|
|
|
|
|
input_display_list = [ 1, 0 ];
|
|
|
|
|
|
|
|
connected_device = 0;
|
2023-07-05 15:09:52 +02:00
|
|
|
port = 0;
|
|
|
|
socket = noone;
|
2023-06-04 12:38:40 +02:00
|
|
|
|
2023-11-22 05:17:13 +01:00
|
|
|
function setPort(newPort) { #region
|
2023-06-04 12:38:40 +02:00
|
|
|
if(ds_map_exists(PORT_MAP, port))
|
|
|
|
array_remove(PORT_MAP[? port], self);
|
|
|
|
|
|
|
|
port = newPort;
|
|
|
|
if(!ds_map_exists(PORT_MAP, port))
|
|
|
|
PORT_MAP[? port] = [];
|
|
|
|
array_push(PORT_MAP[? port], self);
|
|
|
|
|
2023-11-22 05:17:13 +01:00
|
|
|
if(ds_map_exists(NETWORK_SERVERS, port))
|
2023-06-04 12:38:40 +02:00
|
|
|
return;
|
2023-07-05 15:09:52 +02:00
|
|
|
|
|
|
|
if(socket >= 0) network_destroy(socket);
|
2023-11-22 05:17:13 +01:00
|
|
|
socket = network_create_server_raw(network_socket_ws, port, 16)
|
2023-07-05 15:09:52 +02:00
|
|
|
if(socket < 0) return;
|
|
|
|
|
|
|
|
NETWORK_SERVERS[? newPort] = socket;
|
2023-11-22 05:17:13 +01:00
|
|
|
} #endregion
|
2023-06-04 12:38:40 +02:00
|
|
|
|
2023-06-05 18:27:53 +02:00
|
|
|
insp1UpdateTooltip = __txt("Refresh Server");
|
2023-06-04 12:38:40 +02:00
|
|
|
insp1UpdateIcon = [ THEME.refresh, 1, COLORS._main_value_positive ];
|
|
|
|
|
2023-11-22 05:17:13 +01:00
|
|
|
static onInspector1Update = function() { #region
|
2023-10-02 08:57:44 +02:00
|
|
|
var _port = getInputData(0);
|
2023-07-05 15:09:52 +02:00
|
|
|
|
2023-06-04 12:38:40 +02:00
|
|
|
setPort(_port);
|
2023-11-22 05:17:13 +01:00
|
|
|
} #endregion
|
2023-06-04 12:38:40 +02:00
|
|
|
|
2023-06-13 14:42:06 +02:00
|
|
|
network_trigger = 0;
|
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-10-02 08:57:44 +02:00
|
|
|
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 :
|
|
|
|
noti_status($"Websocket server: Client connected at port {port} on node {display_name}");
|
|
|
|
connected_device++;
|
|
|
|
break;
|
|
|
|
case network_type_disconnect :
|
|
|
|
noti_status($"Websocket server: Client disconnected at port {port} on node {display_name}");
|
|
|
|
connected_device--;
|
|
|
|
break;
|
|
|
|
case network_type_data :
|
|
|
|
var buffer = async_load[? "buffer"];
|
|
|
|
var socket = async_load[? "id"];
|
|
|
|
var data = buffer_get_string(buffer);
|
|
|
|
|
2023-06-24 22:12:35 +02:00
|
|
|
var _data = json_try_parse(data, noone);
|
|
|
|
if(_data == noone) _data = { rawData: new Buffer(buffer) }
|
|
|
|
else buffer_delete(buffer);
|
|
|
|
|
|
|
|
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
|
2023-06-13 14:42:06 +02:00
|
|
|
if(network_trigger == 1) {
|
2023-06-04 12:38:40 +02:00
|
|
|
outputs[| 1].setValue(1);
|
2023-06-13 14:42:06 +02:00
|
|
|
network_trigger = -1;
|
|
|
|
} else if(network_trigger == -1) {
|
2023-06-04 12:38:40 +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;
|
2023-10-02 08:57:44 +02:00
|
|
|
var _port = getInputData(0);
|
2023-06-04 12:38:40 +02:00
|
|
|
|
|
|
|
if(port != _port)
|
|
|
|
setPort(_port);
|
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
|
2023-10-02 08:57:44 +02:00
|
|
|
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)
|
2023-09-28 15:10:41 +02:00
|
|
|
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
|
|
|
}
|