From 7302679a8116020c4c795ab12249fcf3e80f9c42 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Tue, 7 Feb 2023 01:22:26 +0100 Subject: [PATCH] Make `websockets.php` able to proceed blocking treatments --- website/websocket.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/website/websocket.php b/website/websocket.php index e06dc3a..26bd521 100644 --- a/website/websocket.php +++ b/website/websocket.php @@ -2,6 +2,8 @@ use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; +use React\EventLoop\LoopInterface; +use React\EventLoop\Timer\Timer; // Make sure composer dependencies have been installed require __DIR__ . '/vendor/autoload.php'; @@ -12,10 +14,12 @@ require __DIR__ . '/vendor/autoload.php'; class MyChat implements MessageComponentInterface { protected $clients; + private $loop; - public function __construct() + public function __construct(LoopInterface $loop) { $this->clients = new \SplObjectStorage(); + $this->loop = $loop; } public function onOpen(ConnectionInterface $conn) @@ -26,6 +30,10 @@ class MyChat implements MessageComponentInterface public function onMessage(ConnectionInterface $from, $msg) { $from->send($msg); + $this->loop->addTimer(Timer::MIN_INTERVAL * 10, function() use ($from) { + sleep(5); + $from->send('Proceeded!'); + }); } public function onClose(ConnectionInterface $conn) @@ -39,7 +47,9 @@ class MyChat implements MessageComponentInterface } } +$loop = \React\EventLoop\Factory::create(); + // Run the server application through the WebSocket protocol on port 4430 -$app = new Ratchet\App('crawler.yt.lemnoslife.com', 4430); -$app->route('/websocket', new MyChat(), array('*')); +$app = new Ratchet\App('crawler.yt.lemnoslife.com', 4430, '127.0.0.1', $loop); +$app->route('/websocket', new MyChat($loop), array('*')); $app->run();