【发布时间】:2015-09-04 04:30:57
【问题描述】:
我想知道有多少客户实际订阅了聊天室/对话。
更准确地说,我只想知道是否有超过 1 个客户。 (聊天室实际上是两个用户之间的私人对话)。
一次只有一个聊天室/私人对话(每个用户)。
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 获得灵感的。
【问题讨论】:
-
给定的代码非常稀疏...
-
好吧,我可以添加更多,这不是我的全部代码,但我试图在这里保持简单,以免造成任何混乱。保存在数据库中的所有逻辑部分都已从该 sn-p 中删除。这段代码实际上是有效的。我不确定前端代码是否有帮助。
-
好吧,这段代码无法用于您的问题。你的线程池在哪里?客户在哪里注册并实例化他们自己的对象?您跟踪的 hashmap/arraylist/linkedqueue 在哪里?
-
您是否尝试过使用
$conversation_id->count()(在将其转换为字符串之前)-如源github.com/ratchetphp/Ratchet/blob/master/src/Ratchet/Wamp/…中所述 -
嗯,这似乎工作。我虽然这将返回客户总数,尽管他们分配了对话。我必须在实际情况下尝试,但我还没有合适的服务器。我建议您将其作为答案提交;)。
标签: php websocket ratchet phpwebsocket