✅ How to clean up a stream of incomplete JSON data?
I'm working on adding some discord capabilities to a bot I'm working on but the data I get is really messy? I seem to just get a constant stream of data but I don't see any actual identifier of when a message ends?
So I was wondering if there is any way that I could possibly keep adding a buffer to a StringBuilder and then try and extract the first json object, add it to a Channel and then remove it from the StringBuilder and keep looping it?
This is 2 bits that I was working on so far.. I've cut out a lot of stuff that didn't work
21 Replies
Here is an example of 2 and half logs of data that I get
_webSocket
is what here? A stream?It's a "ClientWebSocket"
I'm kinda new to networking stuff, but it's whatever my connection to Discord is
What in the
Well I mean... That's just how that class works I guess, yeah
What you can do instead is collect just the bytes and then create the string at the very end
How would I know when I've recieved the entire message though?
When
result
is < buffer.Length
, right?Yeh I suppose so... If at any rare time, a message ends up being the exact size of buffer, it'd break the entire thing though?
I guess it wouldn't break... It just wouldn't process any messages until new ones are sent to make the buffer not match again
So once I know all my Json objects are complete. Is there an easy Json function to extract them all?
I made a channel to put messages in. Thinking of having a task run independently on a seperate thread to just pull from the channel
We usually recommend System.Text.Json for de/serialization
STJ can accept objects like streams to read its json from
But this web socket class is weird...
If you have a suggestion of a better way of doing it, I'm all ears
Like I said, I'm pretty new... So I was just trying to be consistent with using sockets
What is STJ?
System.Text.Json
I don't know too much about discord bots. I usually just use one of the existing discord libraries
I hear Remora.Discord and NetCord are good
Well I'm trying to challenge myself, so I'm not using any bot libs
I want to use Discords API directly
If I wanted to use a lib, I'd just run it in Discord.js anyway which makes everything really simplistic
But I want to learn how to network and these libs bypass all of that
It's just a question of if ClientWebSocket is the right class?
Also if I am sticking with that... Your suggestion, am I meant to be creating a Queue of byte arrays to store the data before turning it all into a string?
I was thinking of a simple list
And
AddRange
on it
Rent the buffer from the arraypoolRent?
You return it when you don't need it anymore
And your call stays allocation free
Do you have an example of implementing that? Because I don't quite understand what that does or how it pairs with my
_webSocket.ReceiveAsync
My idea was something like this
You can probably also just statically allocate a buffer
:Velion_Think: I See what you mean. I don't understand the shared bytes thing but I think I get the general idea of how to get my bot working. I've also never heard of CollectionsMarshal but I can look that up
Thanks for the love :Moon_JJ_Love:
Don't worry about the array pool, it's unnecessary. I'd much more recommend just having one persistent buffer
Like
private static readonly byte[] _buffer = new byte[8192];
And just use that. You're overwriting it all the time anywayOkay so I think this is what you mean
I still need a way to split the json objects though
Just noticed the while condition is wrong
I think I got it! It seems to work :Moon_JJ_UwU: Thanks!
Also I think each message is actually just 1 json object... I think the guild create one is just absolutely huge
Thanks for all the help