So whilst my multi-player game is on hold pending some testing and comms overhaul I decided to write my own twitch bot.
However, I’m making him animated and having him respond to chat commands. So far it’s just a cube that spins and changes colour…
The problem is that, even with a timeout value set, when I’m waiting for a message on the chat the rotation (and therefore, all animation I’m guessing) hangs whilst it’s waiting either for said message or a timeout. As a noob, how would you suggest I continue?
I was thinking about a coroutine (something like “yield return inputstreamer.readline()”… or something…), but would it be better to look into multi-threading?
A quick check shows that there is a problem with variable locking and overwriting (I’m don’t know the actual term) so will probably implement a message queue so that I can work on one message whilst receiving another etc.
Does this sound like a possible avenue for me to continue down?
I’ve never made a twitch bot. What C# type is that inputstreamer, exactly?
I would start by checking the docs for whatever it is, and seeing if there is some sort of “data available” method. Then check that in Update, and only attempt the read when that’s true.
Many thanks for looking at this and replying! Always great to hear from a master such as you.
The streamreader is from System.IO.StreamReader. I’ve just looked at the manual, there doesn’t seem to be a function/method for checking for ‘data available’, otherwise that would be a great route to go down
The way this bot works (this is based from some tutorials that I’m building on), is that it sets up a TcpClient (system.net and system.net.sockets - I think) and then uses StreamReader to attach to the incoming stream from that TcpClient. When the message comes in, it’s handled.
My way around the everlasting hang so far has been to implement a timeout on the streamreader. However this still causes a slight, noticeable hang whilst it’s checking - hence why I’m looking into other possibilities.
You might be able to use the Peek() method to check if any data is available. It won’t work if the stream doesn’t support seeking, but if it does work it’ll return a -1 if no data is available and a character if there is data present.
I would probably pursue multi-threading on this. I would think most Windows HTTP servers use multi-threading.
It might be annoying if Fiber Fred had to wait for Dialup Dave to download a LOLcat video before he got his stock quotes.
I suspect most Unix ones (i.e. Apache) use forking, but that option (AFAIK) is not available on all platforms.
There are also non-blocking sockets out there, but I would probably just use multi-threading.
There’s a famous book by the late W. Richard Stevens about this sort of thing. I think it’s called Unix Network Programming:
Some really cool things for me to go away and look at - let the learning commence!!!
I decided to give a bash at the multi-threading angle and that works. So now I can leave the second thread waiting for messages whilst the main does the animation. When a message comes in Thread2 adds the message to the end of the queue, and then Thread 1 just check said queue each frame for a message. This seems to work… but I’m still gonna check out everything mentioned here!
@Kiwasi Do you mean have the main thread just check for message in a coroutine? And then, as I’ve implemented, have the second thread just waiting on the stream for message? Is that what you mean, sir?
@Ryiah Yes, I’ve come across that function but not tried it yet - I think I read somewhere that Twitch servers don’t support Peek()… but I’ll test myself and then I’ll know fo’ sho’.
@boolfone I’ll look into those books and design options (is that the right term?) that’ll give me some good theory learning XD
@JoeStrout I’ve basically implemented something like this as mentioned in the above paragraphs. Many thanks for your time!