Php voryx thruway多重发布

Php voryx thruway多重发布,php,wamp-protocol,thruway,Php,Wamp Protocol,Thruway,我需要从php脚本发布消息,我可以发布单个消息罚款。但现在我需要在循环中发布不同的消息,无法找到正确的方法,以下是我尝试的: $counter = 0; $closure = function (\Thruway\ClientSession $session) use ($connection, &$counter) { //$counter will be always 5 $session->publish('com.example.hello', ['Hello, world

我需要从php脚本发布消息,我可以发布单个消息罚款。但现在我需要在循环中发布不同的消息,无法找到正确的方法,以下是我尝试的:

$counter = 0;
$closure = function (\Thruway\ClientSession $session) use ($connection, &$counter) {
//$counter will be always 5
$session->publish('com.example.hello', ['Hello, world from PHP!!! '.$counter], [], ["acknowledge" => true])->then(
    function () use ($connection) {
        $connection->close(); //You must close the connection or this will hang
        echo "Publish Acknowledged!\n";
    },
        function ($error) {
        // publish failed
            echo "Publish Error {$error}\n";
        }
    );
};

while($counter<5){

    $connection->on('open', $closure);

    $counter++;
}
$connection->open();
$counter=0;
$closure=函数(\Thruway\ClientSession$session)使用($connection,&$counter){
//$counter将始终为5
$session->publish('com.example.hello',['hello,来自PHP的世界!!!'。$counter],[“acknowledge”=>true])->然后(
函数()使用($connection){
$connection->close();//必须关闭连接,否则将挂起
回显“已确认发布!\n”;
},
函数($error){
//发布失败
echo“发布错误{$Error}\n”;
}
);
};
而($counteron('open',$close);
$counter++;
}
$connection->open();
在这里,我想向订阅者发布$counter值,但该值始终为5,1。是否有一种方法可以在循环之前打开连接,然后在循环中发布消息 2.如何从循环中访问$session->publish()


谢谢!

有几种不同的方法可以实现这一点。最简单的方法是:

$client = new \Thruway\Peer\Client('realm1');
$client->setAttemptRetry(false);
$client->addTransportProvider(new \Thruway\Transport\PawlTransportProvider('ws://127.0.0.1:9090'));

$client->on('open', function (\Thruway\ClientSession $clientSession) {
    for ($i = 0; $i < 5; $i++) {
        $clientSession->publish('com.example.hello', ['Hello #' . $i]);
    }
    $clientSession->close();
});

$client->start();

请注意,$循环现在被传递到客户端构造函数中,而且我去掉了禁用自动重新连接的行(因此,如果出现网络问题,您的脚本将重新连接)。

不会('open')打开(
$connection->on)
只运行一次?我不知道Thruway,但很可能“open”事件只发出一次。这就是为什么不管循环如何,您只发送一条消息。现在我将$connection->open();放入循环中,它可以根据我的需要工作,但每次打开连接可以吗?一般来说,我有一个deamon运行为:while(true){…}并在那里发布消息。根据他们的说法,似乎您需要注册并调用远程过程来发送数据。($session->register()和$session->call())我已经想到了,问题是我可以使用$session->call()来完成这个闭包函数,它只在我执行$connection->open()时运行)谢谢您的回答。我尝试了两个示例,第一个运行良好,但如果我将循环更改为while(1),则它不会以$client->start()的形式发布消息。第二个示例也是如此。如果按原样运行,则它不会以$client->start()的形式发布消息;不会被调用(
$loop = \React\EventLoop\Factory::create();

$client = new \Thruway\Peer\Client('realm1', $loop);
$client->addTransportProvider(new \Thruway\Transport\PawlTransportProvider('ws://127.0.0.1:9090'));

$loop->addPeriodicTimer(0.5, function () use ($client) {

    // The other stuff you want to do every half second goes here

    $session = $client->getSession();

    if ($session && ($session->getState() == \Thruway\ClientSession::STATE_UP)) {
        $session->publish('com.example.hello', ['Hello again']);
    }
});

$client->start();