我使用名爲Philip的PHP bot框架構建了一個IRC bot(https://github.com/epochblue/philip)。當任何人將命令!hello
發送到聊天室時,機器人應在通道中說出「hello ...」,等待45秒,說出「foo」,等待15秒,然後說出「bar」(我知道,沒有任何意義,只是試圖讓這個代碼工作)。使用sleep()延遲所有輸出
下面是我試過到目前爲止代碼:
嘗試#1
$bot->onChannel('/^!hello$/', function($event) {
$event->addResponse(Response::msg($event->getRequest()->getSource(), "hello..."));
$now = time();
while ($now + 45 > time()) {
}
$event->addResponse(Response::msg($event->getRequest()->getSource(), "foo"));
while ($now + 60 > time()) {
}
$event->addResponse(Response::msg($event->getRequest()->getSource(), "bar"));
});
嘗試#2
$bot->onChannel('/^!hello$/', function($event) {
$event->addResponse(Response::msg($event->getRequest()->getSource(), "hello..."));
sleep(45);
$event->addResponse(Response::msg($event->getRequest()->getSource(), "foo"));
sleep(15);
$event->addResponse(Response::msg($event->getRequest()->getSource(), "bar"));
});
與這兩個這些嘗試中,在輸出任何東西之前,機器人會等待整整60秒。所以不是發送一條消息,然後等待,然後發送另一條消息,然後等待,然後發送第三條消息,它只是等待所有60秒,然後發送消息。
任何想法,我怎麼能得到這個工作,因爲我希望它?
感謝
嘗試http://www.php.net/flush每封郵件後 – Reeno
我不知道框架,但看代碼(https://github.com/epochblue/philip/blob/master/ src/Philip/Philip.php),它看起來像你想用send()方法發送單獨的響應(「實際上將數據推回套接字(giggity)」)。 – CompuChip
@Reeno剛剛嘗試過。似乎沒有工作。 – 585connor