3

I'm trying to periodically send a "hello world!" message to all clients connected to the chat-server from the Ratchet tutorial

I will post all of the code here: Chat.php:

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

class Chat implements MessageComponentInterface {
    public $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";
    }

    //this worked but I don't want this behaviour
    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();
    }
}

chat-server.php:

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

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

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

    $server->run();

To test how much of the docs I understood , I added a timer to the server's loop

    <?php
    use Ratchet\Server\IoServer;
    use MyApp\Chat;

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

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


        // My code here
        $server->loop->addPeriodicTimer(5, function () {
          echo  "custom loop timer working !";        
        });


        $server->run();

and it worked fine outputting that string every five seconds after starting the server.

Now I tried doing it like so, trying to send a message to clients stored in the MessageComponentInterface called Chat from the tutorial

$server->loop->addPeriodicTimer(5, function () {        
    foreach ($server->app->clients as $client) {                  
            $client->send("hello client");          
    }
});

But I'm getting that $server->app is NULL which is probably because I'm now inside the function() block .I'm not an expert when it comes to Object oriented PHP, and this little project will sure help me a lot. How can I access the MessageComponentInterface called app property of the server inside the timer and then send data to the clients stored in there?

Cœur
  • 37,241
  • 25
  • 195
  • 267
vlatkozelka
  • 909
  • 1
  • 12
  • 27
  • Add `use ($server)` after `function ()` – Charlotte Dunois Jul 16 '16 at 16:18
  • `Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.` https://secure.php.net/manual/en/functions.anonymous.php – Charlotte Dunois Jul 16 '16 at 16:19
  • `$server` is NULL in the closure because it hasn't been defined in the function scope. Read more about it here https://secure.php.net/manual/en/language.variables.scope.php – Charlotte Dunois Jul 16 '16 at 16:23
  • @CharlotteDunois That worked like a charm.I've always wondered how to do that, I even had such problen in JS once and had to redo a big part to prevent this. (Now wondering if that works in JS :p) . Could you please add an answer so I can accept ? – vlatkozelka Jul 16 '16 at 16:24
  • In JS all global variables are available in child scopes. Though the better option would be to pass it as parameter instead of defining all relevant variables as global. – Charlotte Dunois Jul 16 '16 at 16:29

2 Answers2

4

$server isn't defined in the function scope and variables from the parent scope don't get inherited to the child scope by default. Closures can inherit variables from the parent scope by using the use language construct.

$server->loop->addPeriodicTimer(5, function () use ($server) {        
    foreach ($server->app->clients as $client) {                  
            $client->send("hello client");          
    }
});

More information about anonymous functions (closures): https://secure.php.net/manual/en/functions.anonymous.php
More information about variables scope: https://secure.php.net/manual/en/language.variables.scope.php

Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39
1

After some updates the Client Connections are accessible in the MessageHandler

    $port = 3001;
    $handler = new MessageHandler();


    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                handler 
            )
        ),
        $port
    );
    $server->loop->addPeriodicTimer(0.1, function () use ($handler) {
        handler->doStuff();
    });

    $server->run();

The MessageHandler can be found here. The doStuff method should be implemented in this class:

https://github.com/leorojas22/symfony-websockets/blob/master/src/Websocket/MessageHandler.php

Sebastian Viereck
  • 5,455
  • 53
  • 53