我需要在发送信息的过程中进行一些复杂的计算,但是第一条信息必须在计算后立即发送,应该怎么做?
<?php
namespace AppBundle\WSServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class CommandManager implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
//...
}
public function onClose(ConnectionInterface $connection) {
//...
}
public function onMessage(ConnectionInterface $connection, $msg) {
//...
$connection->send('{"command":"someString","data":"data"}');
//...complicated computing
sleep(10);
//send result
$connection->send('{"command":"someString","data":"data"}');
return;
}
}
启动服务器:
$server = IoServer::factory(
new HttpServer(
new WsServer(
$ws_manager
)
), $port
);