gpt4 book ai didi

php - 如何每1分钟检查一次,然后将其发送给客户端?

转载 作者:行者123 更新时间:2023-12-04 16:13:39 25 4
gpt4 key购买 nike

我的问题是我不知道如何在不需要与客户端交互的情况下检查某些内容。我想每分钟检查一次怪物重生的时间(通过从数据库中获取数据),如果是 - 我想向客户端发送一些消息。我可以向客户端发送一些东西,但是如何每分钟执行特定的代码?目前我的服务器端看起来像这样:

<?php
set_time_limit(0);

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require_once '../vendor/autoload.php';

class Chat implements MessageComponentInterface {
protected $clients;
protected $users;

public function __construct() {
$this->clients = new \SplObjectStorage;






}

public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
$this->users[$conn->resourceId] = $conn;



}

public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
unset($this->users[$conn->resourceId]);
}



public function onMessage(ConnectionInterface $from, $data) {
$from_id = $from->resourceId;
$data = json_decode($data);
$type = $data->type;
switch ($type) {
case 'test':
$user_id = $data->user_id;
$chat_msg = $data->chat_msg;
$response_from = "<span style='color:#999'><b>".$user_id.":</b> ".$chat_msg."</span><br><br>";
$response_to = "<b>".$user_id."</b>: ".$chat_msg."<br><br>";
// Output
$from->send(json_encode(array("type"=>$type,"msg"=>$response_from)));
foreach($this->clients as $client)
{
if($from!=$client)
{
$client->send(json_encode(array("type"=>$type,"msg"=>$response_to)));
}
}
break;
case 'chat':
$user_id = $data->user_id;
$chat_msg = $data->chat_msg;
$response_from = "<span style='color:#999'><b>".$user_id.":</b> ".$chat_msg."</span><br><br>";
$response_to = "<b>".$user_id."</b>: ".$chat_msg."<br><br>";
// Output
$from->send(json_encode(array("type"=>$type,"msg"=>$response_from)));
foreach($this->clients as $client)
{
if($from!=$client)
{
$client->send(json_encode(array("type"=>$type,"msg"=>$response_to)));
}
}
break;
}
}

public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}





$server = IoServer::factory(
new HttpServer(new WsServer(new Chat())),
8080
);
$server->run();
?>

最佳答案

一种解决方案:

写一个unix cron脚本或者windows定时任务每分钟运行一次php代码。

关于php - 如何每1分钟检查一次,然后将其发送给客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61618638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com