php - 为什么 $handler 在回调中获得不同的值(value)?

标签 php websocket rabbitmq

每当我在回调中打印处理程序的客户端时,即使添加了几个客户端,它也会显示为空。

我对 php 很天真,似乎每次我调用回调函数时都会传递新的处理程序对象。

{
    <?php

    $handler=new Handler();

    $server = IoServer::factory(
       new HttpServer(
           new WsServer(
            $handler
           )
       ),
       9090   
    );
    $callback = function($msg) {
    echo " [x] Received ", $msg->body, "\n";
    echo $handler->test."\n";
    };
    $pid=pcntl_fork(); 
    if ( $pid == -1 ) {       
        echo "server start fork failed";
        exit(1);
    } else if ( $pid ) {
        $server->run();
    } 


    $connection = new AMQPStreamConnection('localhost', 5672, 'guest',     'guest');
    $channel = $connection->channel();
    $channel->queue_declare('hello', false, false, false, false);
    $channel->basic_consume('hello', '', false, true, false, false, $callback);
    while(count($channel->callbacks)) {
       $channel->wait();
    echo count($channel->callbacks);
    }

    ?>
}

下面是处理程序文件

{
    <?php
    namespace MyApp;
    use Ratchet\MessageComponentInterface;
    use Ratchet\ConnectionInterface;
    class Handler implements MessageComponentInterface {

    public $clients;  
    public $test="Test";

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

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

    public function onMessage(ConnectionInterface $from, $msg) 
    {            
        $this->test="onMessage";

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

        $this->test="sendAll";
        foreach ($this->clients as $client) {
            $client->send($msg);        
        }
    }

    public function onClose(ConnectionInterface $conn) 
    {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) 
    {     
        $conn->close();
    }
    public function getInstance(){
        if(empty($this->handler))
        {
            $this->handler=new Handler();
        }
        return $this->handler;
    }

    } 

    ?>
}

最佳答案

使用单例。

不是这个:

// class Handler... 
private $handler;
...
public function getInstance(){
    if(empty($this->handler))
    {
        $this->handler=new Handler();
    }
    return $this->handler;
}

$handler=new Handler();

但是这个:

// Changed class Handler... 
private static $handler;
...
public static function getInstance(){
    if(empty(self::$handler))
    {
        self::$handler=new Handler();
    }
    return self::$handler;
}

$handler=Handler::getInstance();

[编辑] Handler::getInstance() 目前不是静态的,在我写下我的答案后发现了这一点。

关于php - 为什么 $handler 在回调中获得不同的值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560148/

相关文章:

javascript - 断开客户端与socket.io id的连接

rabbitmq - AMQP 中持久性和持久性概念的困惑

php - 从数据库中存储的 NSArray 中删除字符

php - 更改 WooCommerce 成员(member)状态

php - 在 xampp 上安装 cake php

django - 如何仅允许登录用户连接到 Django Channels 中的 websocket?

javascript - websockets - 从 websocket 服务器获取消息并将其发送到客户端

grails - 使用Realm + RABBIT MQ进行Groovy Httpbuilder身份验证

rabbitmq - 查询RabbitMQ生产者和消费者的详细信息

php - 使用 CakePHP 和 MySQL 进行搜索