dongmacuo1193 2016-11-27 19:37 采纳率: 0%
浏览 22

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

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?