YouTube_captions_search_engine/website/websocket.php

56 lines
1.4 KiB
PHP
Raw Normal View History

<?php
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';
/**
* Send any incoming messages to all connected clients
*/
class MyChat implements MessageComponentInterface
{
protected $clients;
private $loop;
public function __construct(LoopInterface $loop)
{
$this->clients = new \SplObjectStorage();
$this->loop = $loop;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
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)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
$conn->close();
}
}
$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, '127.0.0.1', $loop);
$app->route('/websocket', new MyChat($loop), array('*'));
$app->run();