no coroutine, juste EnterFrame event with 0 timeout :
local function onEnterFrame(event)
local txt = stcp:receive("\*l")
if txt then
print("RECEIVED : "..txt)
end
end
stcp = socket.tcp()
stcp:connect("socketserver IP","socketserver port")
stcp:settimeout(0)
Runtime:addEventListener( "enterFrame", onEnterFrame )
this is a my first step…
If it may help.
[import]uid: 9079 topic_id: 2877 reply_id: 10271[/import]
Does that lose the connection after the first tcp packet is received?
I need to look at my code but I think the corouting is to enable you to keep the connection open [import]uid: 5354 topic_id: 2877 reply_id: 10274[/import]
The connection is kept opened after. It listens untill the socket is closed.
I understood that coroutines are just used to allow a ‘kind of’ multithreading. That’s all. They can be used with sockets to allow to listen to socket without hanging main process, that’s all.
Here, the enterFrame event is used the same way.
[import]uid: 9079 topic_id: 2877 reply_id: 10276[/import]
Even the coroutine will hang the whole app while the TCP waits, the timeout is the same, so the code is similar to yours.
The coroutine enables you, on the next frame, to resume the routine. Thats all really but in my research on coroutines and TCP (much like copas) there is some reason for using it.
I will need to look a my code, its at home, but im sure there is a valid reason. Maybe its more for the master rather than the client TCP code [import]uid: 5354 topic_id: 2877 reply_id: 10277[/import]
My own solution is similar to Olivier’s at msg #20. My app is using a client connection, and runs a timer loop, except I use the “partial results” from the buffer. In my tests, it seems most messages are received, event using “*a” in the receive.
Since this is a prototype app, I’m not too concerned with precision. Also, this method is non-blocking, so the animation still plays uninterrupted.
I still wish there were more examples of a client connection sending and receiving short messages, at least to validate that what I’m doing is the right way. [import]uid: 4454 topic_id: 2877 reply_id: 10278[/import]
Here’s a demo of what I accomplished with LuaSocket: There os a game client running in the Corona Simulator and there is the game server, which is actually the game AI, running in a terminal window on the Mac. The game server is in pure Lua.