我需要从 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 值,但该值始终为 5, 1.有没有办法在循环之前打开连接,然后在循环中发布消息 2.如何访问 $session->publish() 从环形 ?
谢谢!