onMessage事件中是否可以暂定send

xplay

http协议的worker中,请问有没有办法在onMessage时暂停send,等其它异步数据返回时再send回去?

2883 2 0
2个回答

damao

不调用send就可以了啊,需要的时候再调用

  • 暂无评论
xplay

服务端代码见底部,浏览器访问 http://服务器地址:8088/proxy 时,服务器显示如下错误,不知道问题在哪里。。。

Press Ctrl+C to stop. Start success.
---connection  recv req, and connect to remote...
---remote  connected;
PHP Warning:  strlen() expects parameter 1 to be string, array given in /root/test/Workerman/Protocols/Http.php on line 237

Warning: strlen() expects parameter 1 to be string, array given in /root/test/Workerman/Protocols/Http.php on line 237
PHP Notice:  Array to string conversion in /root/test/Workerman/Protocols/Http.php on line 243

Notice: Array to string conversion in /root/test/Workerman/Protocols/Http.php on line 243
---send data to remote ...

php代码如下:

<?php
use \Workerman\Worker;
use \Workerman\Protocols\Http;
use \Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/../../Workerman/Autoloader.php';
require_once __DIR__ . '/../../Workerman/Protocols/Http.php';

$proxy_addr="http://127.0.0.1:8088/get";

$http_worker = new Worker("http://0.0.0.0:8088");

// 当客户端发送消息过来时,转发给所有人
$http_worker->onMessage = function ($connection, $data)
{
    global $http_worker,$proxy_addr;
    $content='';
    $act=$_SERVER;

    //
    if ($act=='/get'){ //模拟一个远程service
        $connection->send(Time());
    }
    else if($act=='/proxy') {

        $new_conn=new AsyncTcpConnection($proxy_addr.'?'.$_SERVER);
        $new_conn->onConnect =function($conn)use($data){
            echo "---remote  connected;\n";
            //连接上远端后,发送客户端请求给远端
            $conn->send($data);
            echo "---send data to remote ...\n";
        };
        $new_conn->onMessage=function($conn, $buffer)use($connection){
            //收到远端消息后,才发回客户端
            echo "---remote  got msg;\n";
            $connection->send($buffer);
            $conn->close();
            $connection->close();
        };

        $new_conn->connect();
        echo "---connection  recv req, and connect to remote...\n";
        return; //暂时不发消息给客户端
    }
    else {
        $connection->send('wrong request.');
    }
};
Worker::runAll();
?>
  • 暂无评论
年代过于久远,无法发表回答
×
🔝