2023-01-30 22:19:04 +01:00
|
|
|
<?php
|
2023-01-31 00:57:06 +01:00
|
|
|
|
2023-01-30 22:19:04 +01:00
|
|
|
use Ratchet\MessageComponentInterface;
|
|
|
|
use Ratchet\ConnectionInterface;
|
2023-02-07 01:22:26 +01:00
|
|
|
use React\EventLoop\LoopInterface;
|
|
|
|
use React\EventLoop\Timer\Timer;
|
2023-01-30 22:19:04 +01:00
|
|
|
|
2023-01-31 00:57:06 +01:00
|
|
|
// Make sure composer dependencies have been installed
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
2023-01-30 22:19:04 +01:00
|
|
|
|
|
|
|
/**
|
2023-01-31 01:05:09 +01:00
|
|
|
* Send any incoming messages to all connected clients
|
2023-01-30 22:19:04 +01:00
|
|
|
*/
|
2023-01-31 00:57:06 +01:00
|
|
|
class MyChat implements MessageComponentInterface
|
|
|
|
{
|
2023-01-30 22:19:04 +01:00
|
|
|
protected $clients;
|
2023-02-07 01:22:26 +01:00
|
|
|
private $loop;
|
2023-01-30 22:19:04 +01:00
|
|
|
|
2023-02-07 01:22:26 +01:00
|
|
|
public function __construct(LoopInterface $loop)
|
2023-01-31 00:57:06 +01:00
|
|
|
{
|
|
|
|
$this->clients = new \SplObjectStorage();
|
2023-02-07 01:22:26 +01:00
|
|
|
$this->loop = $loop;
|
2023-01-30 22:19:04 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 00:57:06 +01:00
|
|
|
public function onOpen(ConnectionInterface $conn)
|
|
|
|
{
|
2023-01-30 22:19:04 +01:00
|
|
|
$this->clients->attach($conn);
|
|
|
|
}
|
|
|
|
|
2023-01-31 00:57:06 +01:00
|
|
|
public function onMessage(ConnectionInterface $from, $msg)
|
|
|
|
{
|
2023-01-31 01:05:09 +01:00
|
|
|
$from->send($msg);
|
2023-02-07 01:22:26 +01:00
|
|
|
$this->loop->addTimer(Timer::MIN_INTERVAL * 10, function() use ($from) {
|
|
|
|
sleep(5);
|
|
|
|
$from->send('Proceeded!');
|
|
|
|
});
|
2023-01-30 22:19:04 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 00:57:06 +01:00
|
|
|
public function onClose(ConnectionInterface $conn)
|
|
|
|
{
|
2023-01-30 22:19:04 +01:00
|
|
|
$this->clients->detach($conn);
|
|
|
|
}
|
|
|
|
|
2023-01-31 00:57:06 +01:00
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e)
|
|
|
|
{
|
2023-01-30 22:19:04 +01:00
|
|
|
$conn->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 01:22:26 +01:00
|
|
|
$loop = \React\EventLoop\Factory::create();
|
|
|
|
|
2023-01-31 01:05:09 +01:00
|
|
|
// Run the server application through the WebSocket protocol on port 4430
|
2023-02-07 01:22:26 +01:00
|
|
|
$app = new Ratchet\App('crawler.yt.lemnoslife.com', 4430, '127.0.0.1', $loop);
|
|
|
|
$app->route('/websocket', new MyChat($loop), array('*'));
|
2023-01-31 00:57:06 +01:00
|
|
|
$app->run();
|