我有一个websocket应用程序,我正在构建一个游戏,基于Ratchet,它使用React事件循环.在这个脚本的开头,我已经想出了如何实现一个periodictimer,每秒向游戏发送一个脉冲,然后执行ticks和combat rounds.这非常有效.
但是,我最近意识到我还需要添加"滞后"客户端的功能,或者暂停在函数中执行.例如,如果玩家被击晕,或者我希望NPC在回复触发器之前等待1.5秒,以获得更"逼真"的会话感觉.
这个功能是内置在反应库中的,还是我必须通过其他方式实现的?经过一些研究,看起来我可能正在寻找pthreads,看看这个问题/答案:如何在PHP应用程序中使用多线程
为了更清楚我想要实现的目标,请以此代码为例:
function onSay($string)
{
global $world;
$trigger_words = array(
'hi',
'hello',
'greetings'
);
$triggered = false;
foreach($trigger_words as $trigger_word)
{
if(stristr($string, $trigger_word))
{
$triggered = true;
}
}
if($triggered)
{
foreach($world->players as $player)
{
if($player->pData->in_room === $this->mobile->in_room)
{
sleep(1);
$this->toChar($player, $this->mobile->short . " says '`kOh, hello!``'");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然,这不起作用,因为sleep(1)函数将暂停整个服务器进程.
任何见解将不胜感激.谢谢!
更新:我的服务器脚本:
require 'vendor/autoload.php';
require 'src/autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\Socket\Server as Reactor;
use React\EventLoop\Factory as LoopFactory;; …
Run Code Online (Sandbox Code Playgroud)