46 lines
1.0 KiB
PHP
46 lines
1.0 KiB
PHP
<?php
|
|
|
|
use Ratchet\MessageComponentInterface;
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
// 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;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->clients = new \SplObjectStorage();
|
|
}
|
|
|
|
public function onOpen(ConnectionInterface $conn)
|
|
{
|
|
$this->clients->attach($conn);
|
|
}
|
|
|
|
public function onMessage(ConnectionInterface $from, $msg)
|
|
{
|
|
$from->send($msg);
|
|
}
|
|
|
|
public function onClose(ConnectionInterface $conn)
|
|
{
|
|
$this->clients->detach($conn);
|
|
}
|
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e)
|
|
{
|
|
$conn->close();
|
|
}
|
|
}
|
|
|
|
// 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->run();
|