Creating / managing single rooms with Ratchet?

I am making an ios chat application. After studying the necessary technologies and protocols, I decided to try websockets. For reasons, our top stack is based on php, and I learned about ratchet as websockets for PHP. I made a simple chat server for ios front-end from reading documentation . Chat works very well, and it’s also convenient for me. I wanted to know how to create separate private chats. Will a separate instance of the socket event loop run for individual rooms?

  • The sample server I created uses a single event loop to manage user connections and send messages to various connection / user IDs. I really tried to look for private chats, but did not find any information where I could be confident. Do I need to manage each connection / user ID in almost this event loop, for example, to decide which users can communicate with each other directly by controlling the sending of messages? Or is their really a separate way to do this? This is an example of an event loop as a pair of documentation that I implemented:

       require dirname(__DIR__) . '/vendor/autoload.php';
    
       $server = IoServer::factory(
           new HttpServer(
               new WsServer(
                   new Chat()
               )
           ),
           8080
       );
    
       $server->run();
    

, ios/android -/. - , , , , , , .

,

+4
2

?

. . . Chat, / .

, {"cmd":"msg", "message":"Hi", "room": 1}, Chat , . , , , {"cmd":"join", "room": 1}, {"cmd":"leave", "room": 1}

+1

, , , .

WampServerInterface MessageComponentInterface Chat ( ).

, .

:

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){

        echo "Message sent to $conversation_id : $event";

        // ... save in Database or else

        // Send data to conversation
        $this->conversationId->broadcast($message);

    }

}

.

, , , Ratchet Github.

, , autobahn.js ( ).

0

Source: https://habr.com/ru/post/1569042/


All Articles