<?php
|
|
namespace WS;
|
|
use Ratchet\MessageComponentInterface;
|
use Ratchet\ConnectionInterface;
|
|
class Chat implements MessageComponentInterface
|
{
|
protected $clients;
|
|
public function __construct()
|
{
|
$this->clients = new \SplObjectStorage;
|
}
|
|
public function onOpen(ConnectionInterface $conn)
|
{
|
$this->clients->attach($conn);
|
$conn->send("Hello overlay client!");
|
}
|
|
public function onMessage(ConnectionInterface $conn, $msg)
|
{
|
$conn->send("Never talk to me like that again!");
|
}
|
|
public function onClose(ConnectionInterface $conn)
|
{
|
$this->clients->detach($conn);
|
echo "Connection {$conn->resourceId} has disconnected\n";
|
}
|
|
public function onError(ConnectionInterface $conn, \Exception $e)
|
{
|
echo "An error has occurred: {$e->getMessage()}\n";
|
|
$conn->close();
|
}
|
|
public function sendToClients($message)
|
{
|
foreach ($this->clients as $client)
|
{
|
$client->send($message);
|
}
|
}
|
}
|