我必须在发送消息之间进行一些复杂的计算,但是第一个消息在执行后以秒发送.我该如何立即发送?
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
);
该博客讨论了在Ratchet WebSocket服务器中如何实现在发送消息后立即进行复杂计算而不延迟的问题。示例代码展示了在`onMessage`方法中,先发送一条消息,然后执行耗时的计算,最后发送计算结果。这引发了如何避免使用`sleep`导致的延迟并实现即时发送的思考。
2048

被折叠的 条评论
为什么被折叠?



