How to communicate with the WebSockets service

2

I have taken the example of Chat in Socketo.me. I have the chat-server.php :

<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new Chat()
            )
        ),
        8080
    );

    $server->run()

I also have the Chat.php

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class ConnectionStorage extends \SplObjectStorage
{
    public function getClientObject($idConnection){

        $this->rewind();
        while ($this->valid()) {
            $object = $this->current(); // similar to current($s)
            $id = $object->resourceId;
            if ($idConnection === $id) {
                $this->rewind();
                return $object;
            }
            $this->next();
        }
    }
}

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }

public static function  sendMessage($idResource, $msg)
    {
        $client = $this->clients->getClientObject($idResource);
        $client->send($msg);
    }
}

Do everything and it works perfectly. My question is if I have a file on the same server called X.php , how can I refer to the context of the web sockets and send a message to a user I know of his resourceId. Something like X.php :

<?php //X.php

$idResource = $_GET['idSocket'];
$msg = $_GET['mensaje'];

Chat::sendMessage($idResource,$msg);
    
asked by israel 11.04.2017 в 23:20
source

1 answer

0

What you propose for the X.php file does not seem to work since in the call to sendMessage the $ clients property is not initialized (the initialization in the server instance with each new connection)

The implementation that you have set seems correct, as alternatives to do what you say I can think of several things:

1.- store $ clients on disk so that connections can be recovered (it is probably not possible to recover the connection as is, but it is the fastest option)

2.- add the messages to be sent to a file or bbdd and the server sends the pending ones from time to time

3.- enable another port (for example 8081) in the server to which you will send the recipient and the message and the server upon receiving it forwards it to the client through the connection established with the

I think the best options are 2 and 3. The simplest to implement is 2 and the most powerful and versatile is la3 (no you need nothing external to manage the messages to send)

I hope it helps you!

    
answered by 19.04.2017 в 08:08