<?php
|
|
namespace WS;
|
|
use Ratchet\ConnectionInterface;
|
use Ratchet\MessageComponentInterface;
|
|
class Pusher implements MessageComponentInterface
|
{
|
protected $clients;
|
|
public function __construct()
|
{
|
$this->clients = new \SplObjectStorage;
|
}
|
|
public function broadcast($msg)
|
{
|
foreach ($this->clients as $client)
|
{
|
// The sender is not the receiver, send to each client connected
|
$client->send($msg);
|
}
|
}
|
|
public function onOpen(ConnectionInterface $conn)
|
{
|
echo __METHOD__ . "\n";
|
// Store the new connection to send messages to later
|
$this->clients->attach($conn);
|
|
echo "New connection! ({$conn->resourceId})\n";
|
}
|
|
public function onClose(ConnectionInterface $conn)
|
{
|
echo __METHOD__ . "\n";
|
$this->clients->detach($conn);
|
echo "Connection {$conn->resourceId} has disconnected\n";
|
}
|
|
function onMessage(ConnectionInterface $conn, $msg)
|
{
|
echo __METHOD__ . "\n";
|
echo $msg . PHP_EOL;
|
$conn->send("https://www.youtube.com/watch?v=TR3Vdo5etCQ");
|
}
|
|
public function onError(ConnectionInterface $conn, \Exception $e)
|
{
|
echo __METHOD__ . "\n";
|
}
|
}
|