Reputation: 33
First, please look at my code below.
local function receiveData( )
l = client:receive()
print(l)
return l
end
local refNum = receiveData()
local function endTurn( )
waitText = display.newText("Opponent Turn",centerX,allY*.1,native.systemFont,30)
local passer = receiveData()
if passer ~= nil then
takeTurn()
end
end
What I want to do is displaying waitText on screen but I faced the obstruction because waitText will not appear until passer get received data first. So, I would like to know how to display waitText while wait for received data.
I will appreciate any advice, Thanks.
Upvotes: 1
Views: 57
Reputation: 29571
You will have to do two things:
settimeout(seconds)
function on the socket object. This is documented here. Note that you will have to then check the error code when you use receive
, as it could be timeout
to indicate that nothing was available for read in the given time. Something like:
client:settimeout(0.01) -- 10 ms
local function receiveData( )
l,e = client:receive()
print(l)
return l,e
end
function checkReceive()
local passer, err = receiveData()
if passer ~= nil then
takeTurn()
else
timer.performWithDelay(100, checkReceive) -- repeat the check in 100 ms
end
end
local function endTurn( )
waitText = display.newText("Opponent Turn",centerX,allY*.1,native.systemFont,30)
checkReceive() -- returns in 10ms if nothing available, and will be called
-- automatically every 100ms until something arrives
end
The seconds
can have millisecond precision but actual precision that can be honored will vary by system.
Upvotes: 1