php - Ratchet/网络套接字 : How many clients subscribing to an object?

标签 php websocket ratchet phpwebsocket

我想知道有多少客户实际订阅了聊天室/对话。

更准确地说,我只想知道是否有超过 1 个客户端。 (聊天室实际上是两个用户之间的私有(private)对话)。

一次只有一个聊天室/私有(private)对话(每位用户)。

class Chat implements WampServerInterface
{
    protected $conversationId;
    public function __construct(){
        $this->conversationId = null;
    }
    public function onSubscribe(ConnectionInterface $conn, $conversation_id){
        $this->conversationId = $conversation_id;
        echo "Client $conn->resourceId assigned to the conversation : $conversation_id\n";
    }
    public function onPublish(ConnectionInterface $conn, $conversation_id, $event, array $exclude, array $eligible){
        // How to get $nb_clients ?
        echo "$nb_clients User(s) in conversation";
        echo "Message sent to $conversation_id : $event";
        // ...
        $message = $event; 
        // Send data to conversation
        $this->conversationId->broadcast($message);
    }
}

那么在给定的代码中,如何获取$nb_clients?


更新:

我想我开始看到解决方案了。

这是我的第二次尝试:

class Chat implements WampServerInterface
{
    protected $conversation  = array();
    public function onSubscribe(ConnectionInterface $conn, $conversation_id){
        $conversation_id = (string) $conversation_id;
        if(!array_key_exists($conversation_id, $this->conversation)){
            $this->conversation[$conversation_id] = 1;
        }
        else{
            $this->conversation[$conversation_id]++;
        }
        echo "{$this->conversation[$conversation_id]}\n";
        echo "Client $conn->resourceId assigned to the conversation : {$conversation_id}\n";
    }
    public function onUnSubscribe(ConnectionInterface $conn, $conversation_id){
        // Foreach conversations or given conversation remove one client
        $this->conversation[$conversation_id]--;
        echo "$this->conversation[$conversation_id]\n";
        echo "Client $conn->resourceId left the conversation : $conversation_id\n";
    }
    public function onOpen(ConnectionInterface $conn){
        echo "New connection! ({$conn->resourceId})\n";
    }
    public function onClose(ConnectionInterface $conn){
        $this->onUnsubscribe($conn, $this->conversation);
        echo "Connection closed!\n";
    }
    public function onCall(ConnectionInterface $conn, $id, $fn, array $params){
    }
    public function onPublish(ConnectionInterface $conn, $conversation_id, $event, array $exclude, array $eligible){
        $conversation_id = (string) $conversation_id;
        $nb_clients = $this->conversation[$conversation_id]
        echo "$nb_clients User(s) in conversation";
        echo "Message sent to $conversation_id : $event";
        // ...
        $message = $event; 
        // Send data to conversation
        $this->conversation[$conversation_id]->broadcast($message);
    }
    public function onError(ConnectionInterface $conn, \Exception $e){
        echo "An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}

如果这能正常工作,有什么想法吗?它实际上似乎有效,但我仍然不确定它是否是最佳解决方案。我实际上是从 Ratchet github 得到灵感的.

最佳答案

onPublish 的第二个参数是一个Topic 对象(Interface WampServerInterface):

onPublish( Ratchet\ConnectionInterface $conn, string|Ratchet\Wamp\Topic $topic, string $event, array $exclude, array $eligible )

所以根据Ratchet's documentation ,您可以在主题上使用 count() 方法来获取订阅者:

$nb_clients = $conversation_id->count();

关于php - Ratchet/网络套接字 : How many clients subscribing to an object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916990/

相关文章:

javascript - Websocket 无效的帧头

php - pthreads 在 React/Ratchet 中无法按预期工作

php - 加快循环内的查询速度?

php - MYSQL 更新不适用于 php 变量 id

ios - ubuntu 服务器上的 socketio 客户端未与 ios 的套接字 io 客户端连接

javascript - 将 ExpressJS 与 nginx 一起使用时出现 502 Bad Gateway

PHP 响应并行请求

ratchet - 为什么有两个 TableView ?

php - 基于共同的列值将两个数组组合成一个数组

php - 从网络检索数据的桌面程序?