PHP套接字循环挂起在MSG_WAITALL。 我还可以做些什么?

Sorry in advanced as I am new to coding and php. I'm receiving UDP data every couple of minutes. Then I am trying to use a while loop to check the data, then echo something based on what the data is. After a set amount of time, I want to echo something else from a database until new data comes in. The problem is when it checks the socket again, it waits until new data comes in. I want it to instead keep echoing data from the database every x seconds until new data from the socket comes in.

The data comes in fragments with a random number of characters. Currently to get one readable string I am using MSG_WAITALL to receive all the data into something I can use because MSG_DONTWAIT gives me weird results. I am guessing the MSG_WAITALL is what is causing it to stop because the if statement that echos from the database only runs once until new data from the socket comes in.

Hopefully I explained this well enough. What else can I try?


while(1)
{
    $r = socket_recvfrom($sock, $buf, 12, MSG_WAITALL, $remote_ip, 
       $remote_port);
    $word = $word.$buf;
    $startTime = date("Y-m-d H:i:s");

    if($word = example1)
    {
        //do this 
        //add time to timer
    }

    elseif($word = example2)
    {
        //do this
        //add time to timer
    }

    else(currentTime > timer)
    {
        //echo from database
        //add time to timer
    }
}