简体   繁体   中英

How to check something every 1 minute, and then send it to client?

My problem is that I don't know how to check something without the need to interact with the client. I want to check every minute if its time for monster to respawn (by taking data from database), if it is - I want to send some msg to clients. I can send something to client, but how to execute specific code every minute? Currently my server side looks like this:

<?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();
?>

One solution:

Write a unix cron script or windows scheduled task to run the php code every minute.

You need to have an open connections with clients, to send them any data. There is two ways to do it:

  1. Every minute request data from client, const- with 1000 clients you will get 1000 requests per minute

  2. Use websocket. For example use library https://socket.io/ , very simple and easy to start. Cons you need to write this part of site on nodejs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM