为什么phpws onMessage方法被线程阻止?

时间:2016-11-27 19:37:52

标签: php pthreads phpws

这是我尝试多线程的phpws示例:

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;
        }
    }
}

这是Cli_Model_Test1类:

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

这是Cli_Model_Test2类:

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

所以基本的行为应该是当我从浏览器发送带有&#39; test1&#39;它应该触发test1线程,并在发送带有&#39; type2&#39;它应该触发test2线程。它就是这样工作但是test1线程阻塞了onMessage方法,所有发送的消息都在队列中等待。它发生在test1线程内的for循环结束之前。

这是WebSocket服务器的输出:

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

为什么onMessage被阻止?

0 个答案:

没有答案