【问题标题】:Periodically sending messages to clients in Ratchet在 Ratchet 中定期向客户端发送消息
【发布时间】:2016-07-16 16:03:19
【问题描述】:

我正在尝试定期发送“hello world!”向 Ratchet 教程中连接到聊天服务器的所有客户端发送消息

我将在这里发布所有代码: Chat.php:

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    public $clients;

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

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    //this worked but I don't want this behaviour
    public function onMessage(ConnectionInterface $from, $msg) {
        /*$numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }*/
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}

聊天服务器.php:

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new Chat(),
        8080
    );

    $server->run();

为了测试我理解了多少文档,我在服务器的循环中添加了一个计时器

    <?php
    use Ratchet\Server\IoServer;
    use MyApp\Chat;

        require dirname(__DIR__) . '/vendor/autoload.php';

        $server = IoServer::factory(
            new Chat(),
            8080
        );


        // My code here
        $server->loop->addPeriodicTimer(5, function () {
          echo  "custom loop timer working !";        
        });


        $server->run();

并且在启动服务器后每五秒输出一次该字符串效果很好。

现在我尝试这样做,尝试向存储在本教程中称为 Chat 的 MessageComponentInterface 中的客户端发送消息

$server->loop->addPeriodicTimer(5, function () {        
    foreach ($server->app->clients as $client) {                  
            $client->send("hello client");          
    }
});

但是我得到 $server->app 是 NULL 这可能是因为我现在在 function() 块内。当谈到面向对象的 PHP 时,我不是专家,这个小项目将肯定对我有很大帮助。 如何访问计时器内服务器的MessageComponentInterface 称为app 属性,然后将数据发送到存储在其中的客户端?

【问题讨论】:

  • function ()之后添加use ($server)
  • Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct. secure.php.net/manual/en/functions.anonymous.php
  • $server 在闭包中为 NULL,因为它尚未在函数范围内定义。在此处阅读更多信息secure.php.net/manual/en/language.variables.scope.php
  • @CharlotteDunois 这就像一个魅力。我一直想知道如何做到这一点,我什至曾经在 JS 中遇到过这样的问题,不得不重做很大一部分来防止这种情况发生。 (现在想知道这是否适用于 JS:p)。您能否添加一个答案以便我接受?
  • 在 JS 中,所有全局变量都在子作用域中可用。虽然更好的选择是将其作为参数传递,而不是将所有相关变量定义为全局变量。

标签: php web-services websocket ratchet


【解决方案1】:

$server 未在函数作用域中定义,并且默认情况下,来自父作用域的变量不会被继承到子作用域。闭包可以使用use 语言结构从父作用域继承变量。

$server->loop->addPeriodicTimer(5, function () use ($server) {        
    foreach ($server->app->clients as $client) {                  
            $client->send("hello client");          
    }
});

关于匿名函数(闭包)的更多信息:https://secure.php.net/manual/en/functions.anonymous.php
有关变量范围的更多信息:https://secure.php.net/manual/en/language.variables.scope.php

【讨论】:

    【解决方案2】:

    在一些更新之后,可以在 MessageHandler 中访问客户端连接

        $port = 3001;
        $handler = new MessageHandler();
    
    
        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    handler 
                )
            ),
            $port
        );
        $server->loop->addPeriodicTimer(0.1, function () use ($handler) {
            handler->doStuff();
        });
    
        $server->run();
    

    MessageHandler 可以在这里找到。 doStuff 方法应该在这个类中实现:

    https://github.com/leorojas22/symfony-websockets/blob/master/src/Websocket/MessageHandler.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-04
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 2019-04-29
      相关资源
      最近更新 更多