1

I have just made a chat hello world for the Ratchet WAMP + autobahn version 1.
full source code here if you want to see

The JavaScript client send chat message:

           function click_send_btn() {
                 var json_data = {
                    "message": $.trim($("#input_message").val())
                 };
            sess.publish("send_message", json_data, true);
            }

The PHP Ratchet server publish the message:

public function onPublish(\Ratchet\ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
    switch ($topic) {
        case 'http://localhost/enter_room':
            $foundChater = $this->allChater[$conn];
            $newChaterName = $event['username'];
            $foundChater->setChatName($newChaterName);
            break;
        case 'send_message':
            $foundChater = $this->allChater[$conn];
            $event['username']=$foundChater->getChatName();
            break;
    }
    $topic->broadcast($event);
    echo "onPublish {$conn->resourceId}\n";
}

enter image description here

I don't understand why publish with excludeme not working.
In the above 2 firefox, right firefox said: I am bar. The message should not display at himself, but it is.

doc ref: autobahn version 1 javascript publish with excludeme

doc ref: ratchet onpublish

doc ref: ratchet topic broadcast

  • Maybe Ratchet does not support the option. Btw: whats wrong with Thruway + Crossbar? – oberstet Aug 06 '14 at 13:00
  • Hello Mr oberstet, I haven't try them yet, try them tomorrow. thanks a lot. Ha ha, maybe you remember me in another post of PHP server RPC..... Anyway, if not limited to PHP, which server you can suggest will work best with Crossbar? will http://tavendo.com/ have a project of WAMP server? –  Aug 06 '14 at 13:44
  • oh, i just found your sample now https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/beginner/server.py –  Aug 06 '14 at 13:47
  • Crossbar supports multiple languages for creating application components: http://crossbar.io/docs/Choose-your-Weapon/. The code example you linked above is for people who want to implement their own _router_ - which you don't need (since there is Crossbar). – oberstet Aug 06 '14 at 14:20
  • Maybe it's not clear enough: you don't need another "server" besides Crossbar. Crossbar will _host_ (and monitor) your app components, and does directly serve clients (for WebSocket, and also static Web assets - if you want). – oberstet Aug 06 '14 at 14:22
  • I guess Ratchet is support the excludeme, i just var_dump the array $exclude, it does show correct excluded sessionId, my code not working because I haven't handle it, instead , i use the broadcast. I will try to find the other solution asap, eg. connection->event function , but i have to leave now, my office is close. –  Aug 07 '14 at 09:54

1 Answers1

1

I have just fix it.
What a fool I am. I had not handle the parameter "array $exclude"
and I also used the $topic->broadcast($event) to force broadcast to all.
Now I create a function

/**
 * check whitelist and blacklist
 * 
 * @param array of sessionId $exclude -- blacklist
 * @param array of sessionId $eligible -- whitelist
 * @return array of \Ratchet\ConnectionInterface
 */
private function getPublishFinalList(array $exclude, array $eligible) {
    //array of sessionId
    $allSessionId = array();
    $this->allChater->rewind();
    while ($this->allChater->valid()) {
        array_push($allSessionId, $this->allChater->current()->WAMP->sessionId);
        $this->allChater->next();
    }

    //if whitelist exist, use whitelist to filter
    if (count($eligible) > 0) {
        $allSessionId = array_intersect($allSessionId, $eligible);
    }

    //then if blacklist exist, use blacklist to filter
    if (count($exclude) > 0) {
        $allSessionId = array_diff($allSessionId, $exclude);
    }

    //return array of connection        
    $result = array();
    $this->allChater->rewind();
    while ($this->allChater->valid()) {
        $currentConn = $this->allChater->current();
        if (in_array($currentConn->WAMP->sessionId, $allSessionId)) {
            array_push($result, $currentConn);
        }
        $this->allChater->next();
    }
    return $result;
}

in the onPublish, I not use the $topic->broadcast($event) anymore.

    $conn2PublishArray = $this->getPublishFinalList($exclude, $eligible);
    foreach ($conn2PublishArray as $conn2Publish) {
        $conn2Publish->event($topic, $new_event);
    }    

connection class has a method 'even', which can send message to the 'subscriber' directly.
Ratchet.Wamp.WampConnection event method