我有一个 websocket 应用程序,我正在构建一个游戏,它建立在使用 React 事件循环的 Ratchet 上。在这个脚本的开始,我已经想出了如何实现一个周期性定时器,每秒向游戏发送一个脉冲,然后执行滴答声和战斗回合。这很好用。
但是,我最近意识到我还需要添加“滞后”客户端或暂停函数执行的功能。例如,如果玩家被惊呆了,或者我希望 NPC 在回复触发器之前等待 1.5 秒以获得更“真实”的对话感觉。
这个功能是内置在 react 库中的,还是我必须通过其他方式实现的?经过一些研究,看起来我可能正在寻找 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!``'");
}
}
}
}
显然,这不起作用,因为 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;;
$world = new WorldInterface();
class Server implements MessageComponentInterface
{
public function __construct(React\EventLoop\LoopInterface $loop)
{
$update = new Update();
$update->doTick();
$loop->addPeriodicTimer(1, function()
{
$this->doBeat();
});
}
public function onOpen(ConnectionInterface $ch)
{
global $world;
$world->connecting[$ch->resourceId] = $ch;
$ch->CONN_STATE = "GET_NAME";
$ch->pData = new stdClass();
$ch->send("Who dares storm our wayward path? ");
}
public function onMessage(ConnectionInterface $ch, $args)
{
if($ch->CONN_STATE == "CONNECTED")
{
$ch->send("> " . $args . "\n");
$interpreter = new Interpreter($ch);
$interpreter->interpret($args);
}
else
{
$ch->send($args);
$login = new Login($ch, $args);
$login->start();
}
}
public function onClose(ConnectionInterface $ch)
{
global $world;
if(isset($ch->pData->name))
{
if(isset($world->players[$ch->pData->name]))
{
echo "Player {$ch->pData->name} has disconnected\n";
unset($world->players->{$ch->pData->name});
}
}
if(isset($world->connecting->{$ch->resourceId}))
{
echo "Connection " . $ch->resourceId . " has disconnected.";
unset($world->connecting->{$ch->resourceId});
}
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
public function doBeat()
{
global $world;
++$world->beats;
foreach($world->process_queue as $trigger_beat => $process_array)
{
// if the beat # that the function should fire on is less than,
// or equal to the current beat, fire the function.
if($trigger_beat <= $world->beats)
{
foreach($process_array as $process)
{
$class = new $process->class();
call_user_func_array(array($class, $process->function), $process->params);
}
// remove it from the queue
unset($world->process_queue[$trigger_beat]);
}
// else, the beat # the function should fire on is greater than the current beat,
// so break out of the loop.
else
{
break;
}
}
if($world->beats % 2 === 0)
{
$update = new Update();
$update->doBeat();
}
}
}
$loop = LoopFactory::create();
$socket = new Reactor($loop);
$socket->listen(9000, 'localhost');
$server = new IoServer(new HttpServer(new WsServer(new Server($loop))), $socket, $loop);
$server->run();