Sockets and Threads

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
roastchicken
Prole
Posts: 5
Joined: Mon Dec 28, 2015 3:37 pm

Sockets and Threads

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Sockets and Threads

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Sockets and Threads

Post 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).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
roastchicken
Prole
Posts: 5
Joined: Mon Dec 28, 2015 3:37 pm

Re: Sockets and Threads

Post 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.
Pie
Prole
Posts: 7
Joined: Mon Feb 15, 2016 7:42 am

Re: Sockets and Threads

Post by Pie »

How did you login to an account with Love?
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests