Greetings,
As a first Corona/Lua attempt, I’m trying to build a little chat client, but am getting hung up on keeping the application responsive while also receiving data from server in a timely fashion.
I’ve looked at the Non-Preemptive Multithreading chapter (http://www.lua.org/pil/9.4.html) but I can’t seem to get it to work the way I expect for my situation.
Any help is appreciated.
After logging in or sending a message, I’m doing:
get( connection )
dispatcher()
My modifications to the 9.4 code.
[code]
function readLobbyConnection (LobbyConnection)
–local c = assert(socket.connect(host, 80))
–local count = 0 --counts the number of bytes read
–c:send(“GET " … file … " HTTP/1.0\r\n\r\n”)
while true do
local lobbytext, status = receive(LobbyConnection)
–count = count + string.len(s)
if status == “closed” then
break
end
end
LobbyConnection:close()
–print(file, count)
chatText.text = lobbytext
end
function receive (connection)
connection:timeout(0) – do not block
local receivedstring, status = connection:receive(2^10)
if status == “timeout” then
coroutine.yield(connection)
end
return receivedstring, status
end
threads = {} – list of all live threads
function get (connection)
– create coroutine
local co = coroutine.create(function ()
readLobbyConnection(connection)
end)
– insert it in the list
table.insert(threads, co)
end
function dispatcher ()
while true do
local threadcount = table.getn(threads)
if threadcount == 0 then break end – no more threads to run
local connections = {}
for threadindex = 1 , threadcount do
local status, res = coroutine.resume(threads[threadindex])
if not res then – thread finished its task?
table.remove(threads, threadindex)
break
else – timeout
table.insert(connections, res)
end
end
if table.getn(connections) == threadcount then
socket.select(connections)
end
end
end
[/code] [import]uid: 135417 topic_id: 29884 reply_id: 329884[/import]