2015-06-18 11 views
6

Ich würde gerne wissen, wie viele Kunden tatsächlich einen Chat/Chat abonnieren.Ratchet/Websockets: Wie viele Kunden abonnieren ein Objekt?

Um genauer zu sein, ich möchte nur wissen, ob es mehr als 1 Client gibt. (Chatroom sind eigentlich eine private Konversation zwischen zwei Benutzern).

Es gibt jeweils nur einen Chat/private Unterhaltung (pro Benutzer).

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); 
    } 
} 

Also in dem gegebenen Code, wie man $ nb_clients bekommt?


Update:

Ich glaube, ich fange an, eine Lösung zu finden.

Hier mein zweiter Versuch:

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(); 
    } 
} 

Irgendwelche Ideen, wenn das funktionieren würde richtig? Es scheint tatsächlich zu funktionieren, aber ich bin mir immer noch nicht sicher, ob es die beste Lösung ist. Ich habe mich von Ratchet github inspirieren lassen.

+0

Der angegebene Code ist sehr spärlich ... – Tschallacka

+0

Nun, ich kann mehr hinzufügen, das ist nicht mein ganzer Code, aber ich habe versucht, es hier einfach zu halten, um keine Verwirrung zu bringen. Der gesamte in der Datenbank gespeicherte Logikteil wurde aus diesem Snippet entfernt. Dieser Code funktioniert tatsächlich. Ich bin mir nicht sicher, dass der Frontend-Code helfen würde. – Brieuc

+0

Nun, dieser Code ist unbrauchbar für Ihre Frage. Wo ist dein Thread-Pool? Wo werden Clients registriert und ihr eigenes Objekt instanziiert? wo ist die hashmap/arraylist/linkedqueue, wo du den Überblick behältst? – Tschallacka

Antwort

4

Das zweite Argument von onPublish ist ein Topic Objekt (Interface WampServerInterface):

onPublish (Ratchet \ ConnectionInterface $ conn, string | Ratchet \ Wamp \ Thema $ Thema, string $ Ereignis, array $ ausschließen, Array $ qualifizierten)

so nach Ratchet's documentation, können Sie die count() Methode zum Thema verwenden, um die Abonnenten zu erhalten:

$nb_clients = $conversation_id->count(); 
Verwandte Themen