Page 1 of 1

Sockets and Threads

Posted: Mon Feb 22, 2016 7:55 am
by roastchicken
Hi everyone,

I'm trying to create a simple Twitch bot with LÖVE2D. I have a working one that simply connects to an IRC channel and prints the messages, but the problem is that it is frozen for the majority of the time. LuaSockets (the sockets library I'm using), and I assume most socket libraries, wait for a response from the server before continuing execution. This is a problem because it means my drawing is delayed by the time-out from the response. I guess I could set a very small time-out but I feel like using threads would be a better way to accomplish this.

The issue I'm having with running this on a thread is that (as far as I know and have experienced) channels can only send basic data types like booleans, numbers, strings, (flat) tables, etc. LuaSockets works by you creating a TCP client, connecting the client to the server, and then sending/receiving data. I'm not exactly sure what the TCP client is, I assume it's a (non-flat) table. Whatever it is, my program stops responding if I try to demand it from a channel. What I'm wondering is if there is any possible way for me to share this client between the main code and the thread's code.

Re: Sockets and Threads

Posted: Mon Feb 22, 2016 9:45 am
by bartbes
Not really. But can't you just do all the socket-stuff in a thread, then send the data back instead? Also, the way to work with luasocket asynchronously is using either socket.select or setting the timeout to 0.

Re: Sockets and Threads

Posted: Mon Feb 22, 2016 12:24 pm
by zorg
As bartbes said, you'd want to pass the messages you want to send via sockets, and the stuff you get back, through channels; those should be either strings, or flat tables (containing strings and numbers and bools at most).

Re: Sockets and Threads

Posted: Mon Feb 22, 2016 1:15 pm
by roastchicken
I was originally wanting to send messages to the IRC channel in addition to receiving them, but now that I think of it, I could just send those from the thread as well. Thanks for the help.

Re: Sockets and Threads

Posted: Tue Feb 23, 2016 3:39 am
by Pie
How did you login to an account with Love?