【发布时间】:2015-08-25 13:16:39
【问题描述】:
我必须在发送消息之间进行一些复杂的计算,但第一条消息是在计算后发送的第二条消息。我怎样才能立即发送?
<?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 compulting
sleep(10);
//send result
$connection->send('{"command":"someString","data":"data"}');
return;
}
}
启动服务器:
$server = IoServer::factory(
new HttpServer(
new WsServer(
$ws_manager
)
), $port
);
【问题讨论】:
-
您可以使用每毫秒运行一次的 EventLoop,以及您自己的要发送的消息队列。
-
这是个好主意,但我认为这不是最佳解决方案(很多迭代,什么都不做)。不幸的是,我不知道任何更好的方法。
-
是的,这是一种最后的建议。没有覆盖 Ratchet 的一些核心部分。我想你可以使用 symphony 来启动一个新的进程来做计算?