user3480543
user3480543

Reputation: 33

How can I realtime receive message from server

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

Answers (2)

Oliver
Oliver

Reputation: 29571

You will have to do two things:

  1. Set a timeout on your socket via the 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.
  2. Setup a timer function to continue reading the socket at regular interval, until the data arrives

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

iBad
iBad

Reputation: 286

May be it is underneath other object? try waitText:toFront()

Upvotes: 0

Related Questions