What's the best way to send organised strings to client?
8 replies, posted
Hello again :smile:
I'm making a little mod, a bit like a shout box on a webpage where players can post stuff and it sticks around and is saved into a sqlite table. I have some code that gets the latest posts from the database table in order of newest to oldest. What's the best way to send this data to the client? Should I do a separate net.Send for each post sent to the client or can they all be sent in one? If so, how do I do it?
Thanks <3
You could send the table?
I don't know how efficient that is though.
Create a table and send it, and then unpack the table.
[QUOTE=ms333;41743529]You could send the table?
I don't know how efficient that is though.[/QUOTE]
How though? The wiki is, as expected, useless
What I'd do is have a "header" for the net message that tells you how many posts you're sending to the client. An unsigned 8-bit integer is good for that (8-bit means it'll send at max a value of 255, but it's slightly more effecient).
Then after writing the header, you'd consecutively send each post (the same amount of posts that you specified in the header) and then send it.
On the client then, you'd first read the header to know how many loops to go through, then in the loop you'd just read each string and add them to a table.
That's the most effecient way.
[QUOTE=EvacX;41743559]What I'd do is have a "header" for the net message that tells you how many posts you're sending to the client. An unsigned 8-bit integer is good for that (8-bit means it'll send at max a value of 255, but it's slightly more effecient).
Then after writing the header, you'd consecutively send each post (the same amount of posts that you specified in the header) and then send it.
On the client then, you'd first read the header to know how many loops to go through, then in the loop you'd just read each string and add them to a table.
That's the most effecient way.[/QUOTE]
Do people actually say 8-bit? It's one byte.
Anyway you didn't really explain [I]why[/I] this is the best way, so I'll fill you in there.
net.WriteTable() writes 1 byte for each value in the table representing the type of that value.
[lua]
function net.WriteTable( tab )
for k, v in pairs( tab ) do
net.WriteType( k )
net.WriteType( v )
end
-- End of table
net.WriteUInt( 0, 8 )
end
[/lua]
If you know what's meant to be sent, it's more efficient to do what EvacX said :
Server :
[lua]
net.Start( "myconnection" );
net.WriteUInt( #mytableofstrings, 8 );
for _, value in pairs( mytableofstrings ) do net.WriteString( value ); end
net.Send( someplayer );
[/lua]
Client :
[lua]
net.Recieve( "myconnection", function( len )
local amount = net.ReadUInt( 8 );
local table = {};
for i = 1, amount do
table[ i ] = net.ReadString();
end
end);
[/lua]
[url=http://wiki.garrysmod.com/page/net/Send]net.Send[/url]
[url=http://wiki.garrysmod.com/page/net/Recieve]net.Recieve[/url]
[url=http://wiki.garrysmod.com/page/net/ReadUInt]net.ReadUInt[/url]
[url=http://wiki.garrysmod.com/page/net/WriteUInt]net.WriteUInt[/url]
[url=http://wiki.garrysmod.com/page/net/ReadString]net.ReadString[/url]
[url=http://wiki.garrysmod.com/page/net/WriteString]net.WriteString[/url]
[QUOTE=>>oubliette<<;41744093]Do people actually say 8-bit? It's one byte.[/QUOTE]
That's just a bit silly, you don't for example say "1-byte color" when you're talking about [url=http://en.wikipedia.org/wiki/8-bit_color]"8-bit color"[/url]. And said function accepts a "bit" parameter regardless, so it's more convenient to mention it as such.
[QUOTE=EvacX;41744153]That's just a bit silly, you don't for example say "1-byte color" when you're talking about [url=http://en.wikipedia.org/wiki/8-bit_color]"8-bit color"[/url]. And said function accepts a "bit" parameter regardless, so it's more convenient to mention it as such.[/QUOTE]
Rather than saying an "8-bit unsigned integer" I'd just say a "unsigned byte". I suppose the context matters.
Thanks heaps guys :dance:
Sorry, you need to Log In to post a reply to this thread.