[Script Module] SendClientData / SendServerData

Started by DizzasTeR, Jul 21, 2020, 07:57 AM

Previous topic - Next topic

DizzasTeR

Easily send data from server to client(s) or from client to server. Without having to use a thousand "Stream Types" or using stupid concatenations of strings and splitting.

This can work for official plugin as well but you will have to do some necessary editing (which is a few lines, I might look into that and do it myself later perhaps)

Use it carefully, it COULD have issues, I did as much thorough testing as I could, but still.

Supports:
- Simple data types (integers, strings, bool, null)
- Static arrays (These are arrays made via local myVar = [];)
- Tables

— Server module
Attachment file: SqClient
Provides function: SendClientData( [Array of players to send it to, empty array = send to all], param1, param2, ... );

Load this file from sqmod.ini
Note: You need to have this function somewhere defined, from this you can extract data, and even setup custom signal calls
function onServerReceiveClientData(player, ...) {
local data = vargv;
SqLog.Inf("onServerReceiveClientData");
}

— Client module
Attachment file: DClientData.nut
Provides function: SendServerData( param1, param2, ... );

Load this file as first from every other client file (Thats how I did my testings) — include("DClientData.nut");

Note: Make sure you setup a stream type like this for the magic to happen:
function Server::ServerData( stream )
{
    // here you receive the data you send from server
    local streamType = stream.ReadInt();
    switch( streamType )
    {
        case 0x64697a7a:
        {
onClientDecodeData(stream);
        }
        break;
    }
}

Note: Make sure you have this function defined somewhere in client side:
function onClientReceiveData(...) {
}

— Usage

Server:
SqCore.On().PlayerSpawn.Connect(function(player, ...) {
local Table = {
DizzasTeR = "IQ 900",
IQ = 900,
Abuse = ["This is too much"]
};
local Array = [Table, "Client System", "Swag Level", 9000];
SendClientData([player], "Request", "Advanced", 100, Array);
});

function onServerReceiveClientData(player, ...) {
local data = vargv;
// Going through the data array and extracting data
}

Client
function onClientReceiveData(...) {
local param1 = vargv[0];
local ResponseTable = {
Message = "Sending data to server!",
MyCode = 1
}
if(param1 == "Request") {
SendServerData("Response", ResponseTable);
}
}

In Action



("Request" + "Advanced" + 100 + Array = 4 elements)


("Response" + ResponseTable = 2 elements)

P.S: That stream type number is not random :D