Polling a LuaSocket - attempting a chat client

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]

I think your code is blocking. The general idea for cooperative threads is to never block. Your code must end so Corona has a chance to continue its normal run loop.

You run some code in a coroutine then yield (pause) after a certain amount of time so it doesn’t seem unresponsive. The coroutine transfers back to the calling main routine. But that original main routine needs to end to let Corona continue processing.

My hunch is that you need to need to structure your code so that your coroutine.resume calls are fired periodically. Try calling your resume or dispatcher in an enterframe listener.

[import]uid: 7563 topic_id: 29884 reply_id: 119804[/import]

enterframe listener. OK.

Thanks! [import]uid: 135417 topic_id: 29884 reply_id: 119812[/import]

I think your code is blocking. The general idea for cooperative threads is to never block. Your code must end so Corona has a chance to continue its normal run loop.

You run some code in a coroutine then yield (pause) after a certain amount of time so it doesn’t seem unresponsive. The coroutine transfers back to the calling main routine. But that original main routine needs to end to let Corona continue processing.

My hunch is that you need to need to structure your code so that your coroutine.resume calls are fired periodically. Try calling your resume or dispatcher in an enterframe listener.

[import]uid: 7563 topic_id: 29884 reply_id: 119804[/import]

enterframe listener. OK.

Thanks! [import]uid: 135417 topic_id: 29884 reply_id: 119812[/import]