• Simple net library question
    10 replies, posted
I have a table on the server, and I want to convert it to a JSON string, compress it, then send via the net library's WriteData function. The WriteData and ReadData functions have a length argument. Does anyone know what to put in these fields? [CODE] // server local tab = { }; tab = util.TableToJSON( tab ); tab = util.Compress( tab ); net.WriteData( tab, question_mark ); // client local tab = net.ReadData( question_mark ); tab = util.Decompress( tab ); tab = util.JSONToTable( tab ); PrintTable( tab ); [/CODE]
You want to use net.WriteString and net.ReadString. net.WriteData and net.ReadData are for binary data only.
I know, but my tables are becoming too large. I figured it out by just using the length of the compressed string. Quote from this thread: [URL]http://www.facepunch.com/showthread.php?t=1407671[/URL] [QUOTE=Internet1001;45327912]Not really, you have to decrease the amount of data you need to send. You could use a library like [url=http://www.facepunch.com/showthread.php?t=1194008]vON[/url] to serialize your data, then [url=http://wiki.garrysmod.com/page/util/Compress]util.Compress[/url] to compress the data and then send it via net.WriteData() and do the opposite process on the receiving end. Here's how I do it Sending [lua]local data = von.serialize( tbl ) -- serialize data = util.Compress( data ) -- compress net.Start( "netmsg_Msg" ) net.WriteUInt( #data, 32 ) -- Write the length of the data net.WriteData( data, #data ) -- Write the data net.Send( sendto )[/lua] Receiving [lua]local len = net.ReadUInt( 32 ) -- Get the length of the data data = net.ReadData( len ) -- Read the data with the correct length data = util.Decompress( data ) -- Decompress data = von.deserialize( data ) -- deserialize[/lua][/QUOTE]
[QUOTE=AtomicTACO;45394508]I know, but my tables are becoming too large. I figured it out by just using the length of the compressed string.[/QUOTE] Just wondering, what do your tables contain? Maybe there is a better way of handling the data, Does the client really need the entire table?
The table is generated by a think function that gets the bone positions/angles, and time at which they were retrieved of a ragdoll over a certain period of time.
[QUOTE=AtomicTACO;45394535]The table is generated by a think function that gets the bone positions/angles, and time at which they were retrieved of a ragdoll over a certain period of time.[/QUOTE] Hmm, couldn't you do that in a shared, for both client and server, I do believe you can use those functions shared?
I could do it on the server, or on each client.. I like the idea of doing it on the server, then sending everyone the replay data. Eh, hopefully, it works.
[QUOTE=AtomicTACO;45394610]I could do it shared, or do it on the client. I like the idea of doing it on the server, then sending everyone the replay data. Eh, hopefully, it works.[/QUOTE] Run some tests, run it shared, see if they both output the same (They should) That way every client and the server all have the same data without networking anything.
[URL]https://www.youtube.com/watch?v=Jn5ygaxGkfQ[/URL] - I ended up making it all clientside. Soon I'll add support to replay sounds, props, etc..
[QUOTE=AtomicTACO;45394242]I have a table on the server, and I want to convert it to a JSON string, compress it, then send via the net library's WriteData function. The WriteData and ReadData functions have a length argument. Does anyone know what to put in these fields? [CODE] // server local tab = { }; tab = util.TableToJSON( tab ); tab = util.Compress( tab ); net.WriteData( tab, question_mark ); // client local tab = net.ReadData( question_mark ); tab = util.Decompress( tab ); tab = util.JSONToTable( tab ); PrintTable( tab ); [/CODE][/QUOTE] Length is the number of bytes. --server net.WriteData( tab, #tab ); --client net.Receive("name", function(netlen) local tab = net.ReadData(netlen/8) end)
[media]http://www.youtube.com/watch?v=KpaO0QtEp88[/media] almost got it working. Thanks for the help guys
Sorry, you need to Log In to post a reply to this thread.