1

I am setting up a WampServer with Ratchet. Is it possible to add to the loop a timer that calls a WampServer's method every 30 second?

I have tried the following code:

public function addMonitoringTimer(){

    $this->loop->addPeriodicTimer(30, function() {
        ...
        $this->wampServer->methodName();
        ...
    });

} 

but no timers seems to work.

Note: As this code is a class method, $this is a reference to the class object that has references to the WampServer ($this->wampserver) and the loop used by the WampServer ($this->loop). The method I am calling is not part of the WampServerInterface.

1 Answers1

-1

Let's say Pusher is the clas that implements the WampServerInterface. We define a custom (not part of the interface) method onMessageToPush() in Pusher.

class Pusher implements WampServerInterface {
    ...
    public function onMessageToPush(){
        ...
    }
    ...
}

Now, create a React loop:

$loop  = \React\EventLoop\Factory::create();

, we setup a websocket server object:

$webSock = new \React\Socket\Server($loop);
$webSock->listen($bindPort, $bindIp);

, we create the WampServer object:

$pusher = new Pusher();
$wampServer = new \Ratchet\Wamp\WampServer(
    $pusher
);

we setup an I/O server using the above wamp server, web socket and loop:

$ioserver = new \Ratchet\Server\IoServer(
      new \Ratchet\Http\HttpServer(
        new \Ratchet\WebSocket\WsServer(
                $wampServer
            )
      ),
      $webSock,
    $loop
);

and now we can define a timer that will call our custom method:

$loop->addPeriodicTimer(30, function() use ($pusher) {
        $message = "my message";
        $pusher->onMessageToPush($message);
});

For everyone that may be interested in this, I have built an example that illustrates how to implement some functionality with Ratchet, including the above functionality You can find it here:

Example of using Ratchet

  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/14346856) – Franz Gleichmann Nov 20 '16 at 18:59
  • Sorry, but here http://stackoverflow.com/help/self-answer says that editing the question is as valid as posting the answer. Am I missing something? – Alexandros Gougousis Nov 20 '16 at 22:39
  • @FranzGleichmann What do you mean? I was the author and the example that I provide does call a custom method through a timer. Should I point out more clearly which part of this small example does the job (calling the custom method through a timer)? – Alexandros Gougousis Nov 20 '16 at 22:44