Why phpws onMessage method get blocked by the thread?

74 views Asked by At

This is phpws example where I'm trying multithreading:

class Cli_TestHandler extends WebSocketUriHandler
{
    public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg)
    {
        $dataIn = Zend_Json::decode($msg->getData());

        switch ($dataIn['type']) {
            case 'test1':
                $test1 = new Cli_Model_Test1();
                $test1->start();
                echo 'END';
                break;
            case 'test2':
                $test2 = new Cli_Model_Test2();
                $test2->start();
                break;
        }
    }
}

This is Cli_Model_Test1 class:

class Cli_Model_Test1 extends Thread
{
    public function run()
    {
        for ($i = 0; $i < 10; $i++) {
            sleep(10);
            echo 1;
        }
    }
}

and this is Cli_Model_Test2 class:

class Cli_Model_Test2 extends Thread
{
    public function run()
    {
        echo 2;
    }
}

So basic behavior should be that when I send message from the browser with type 'test1' than it should trigger test1 thread and when send message with 'type2' it should trigger test2 thread. And it is working like that but test1 thread is blocking onMessage method and all messages that were send are waiting in the queue. It happens until the end of for loop inside test1 thread.

This is the output of WebSocket server:

2016-11-27T20:31:03+01:00 NOTICE (5): phpws listening on tcp://127.0.0.1:8080                                                                                                                                                                  
2016-11-27T20:31:03+01:00 DEBUG (7): Got an HYBI style request, sent HYBY handshake response                                                                                                                                                   
2016-11-27T20:31:03+01:00 NOTICE (5): Added client connection-583b347776c42 to Cli_TestHandler                                                                                                                                                 
END 20:33:38
1 19:33:48
1 19:33:58
1 19:34:08
1 19:34:18
1 19:34:28
1 19:34:38
1 19:34:48
1 19:34:58
1 19:35:08
1 19:35:18
2 19:35:18

Why onMessage is blocked?

0

There are 0 answers