web服务-我如何访问棘轮php周期性循环和应用程序内部的客户端发送



我有一个Ratchet服务器和聊天应用程序类,运行良好。我的问题是如何添加周期性循环?

我试着效仿Ratchet 中定期向客户发送台面的例子

但我什么也没得到。我的目标和这个家伙一样,是让服务器检查所有客户端是否还活着。每次我尝试使用addPeriodicTimer时,我似乎都无法像上面链接中的那个人那样访问chat.php中的$client公共属性,以便从server.php中的计时器发送消息。server.php中周期计时器中的foreach循环不断抱怨它显然有一个"无效参数"。

有人看到我做错了什么吗?

我的服务器php代码:

<?php

require($_SERVER['DOCUMENT_ROOT'].'/var/www/html/vendor/autoload.php');
require_once($_SERVER['DOCUMENT_ROOT']."/var/www/html/bin/chat.php");
use RatchetServerIoServer;
use RatchetHttpHttpServer;
use RatchetWebSocketWsServer;
use RamChat;

$server = IoServer::factory(new HttpServer(new WsServer(new Chat())), 8080);
 // Server timer <------ having trouble here
$server->loop->addPeriodicTimer(5, function () use ($server) {
foreach($server->app->clients as $client)
{
    //$client->send("[helloworld]");    
}
});

$server->run();
?>

和我的聊天.php:

<?php
namespace Ram;
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;

error_reporting(E_ALL ^ E_NOTICE);
session_id($_GET['sessid']);
    if(!session_id)
        session_start();

    $userid = $_SESSION["userid"];
    $username = $_SESSION["username"];
    $isadmin = $_SESSION["isadmin"];
    $resources = array();


    class Users
    {
        public $name;
        public $resid;
        public $timestamp;

    }

class Chat implements MessageComponentInterface
{
    public $clients;




    var $users = array();

    /*
    function cmp($a, $b)
    {
        return strcmp($a->name, $b->name);
    }

    function removeObjectById(ConnectionInterface $id , $arr)
    {
         $array = $arr;
        foreach ( $array as $key => $element ) {
            if ( $id->resourceId == $element->resid ) 
            {
                unset($array[$key]);
                break;
            }
         }
         usort($array, "cmp");
         return $array;
    }
    */


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



    }
    public function onOpen(ConnectionInterface $conn)
    {
        $this->clients->attach($conn);  


    }
    public function onClose(ConnectionInterface $conn)
    {
        //$users = removeObjectById($conn, $users);
        $this->clients->detach($conn);

    }
    public function onMessage(ConnectionInterface $conn, $msg)
    {
         $msgjson = json_decode($msg);
         $tag = $msgjson->tag;
         if($tag == "[msgsend]")
         {
            foreach($this->clients as $client)
            {
                  $client->send($msg);    
            }
     }
     else if($tag == "[bye]")
     {
         foreach($this->clients as $client)
         {
              $client->send($msg);    
         }
         $this->clients->detach($conn);
     }
     else if($tag == "[connected]")
     {
         //store client information
         $temp = new Users();
         $temp->name = $msgjson->uname;
         $temp->resid = $conn->resourceId;
         $temp->timestamp = date('Y-m-d H:i:s');
         $users[] = $temp;
         //usort($users, "cmp");

         //send out messages
          foreach($this->clients as $client)
         {
              $client->send($msg);    
         }

     }
     else if($tag == "[imalive]")
     {
         //update user timestamp who sent [imalive]
         if (is_array($users) || is_object($users))
         {
             foreach($users as $user)
             {
                if($msgjson->uname == $user->name)
                {
                        $user->timestamp = date('Y-m-d H:i:s'); 
                }
             }
         }
     }



}
public function onError(ConnectionInterface $conn, Exception $e)
{
    echo "Error: " . $e->getMessage(); 
    $conn -> close();   
}
}
?> 

为什么不在传递到HTTPServer:之前定义Chat对象实例

$chat = new Chat();
$server = IoServer::factory(new HttpServer(new WsServer($chat)), 8080);
// Server timer <------ having trouble here
$server->loop->addPeriodicTimer(5, function () use ($chat) {
foreach($chat->clients as $client)
{
    //$client->send("[helloworld]");    
}
});

$server->run();
<?php

require($_SERVER['DOCUMENT_ROOT'].'/var/www/html/vendor/autoload.php');
require_once($_SERVER['DOCUMENT_ROOT']."/var/www/html/bin/chat.php");
use RatchetServerIoServer;
use RatchetHttpHttpServer;
use RatchetWebSocketWsServer;
use RamChat;

$server = IoServer::factory(new HttpServer(new WsServer($chat = new Chat())), 8080);
 // Server timer <------ having trouble here
$server->loop->addPeriodicTimer(5, function () use ($chat) {
foreach($chat->clients as $client)
{
//    $client->send("[helloworld]");    
}
});

$server->run();
?>

相关内容

  • 没有找到相关文章

最新更新