Fibinger Ádám
2020-06-04 ffa7c6f5c007dfc826aef0ca27d23bb69bbeed7e
commit | author | age
39aea3 1 <?php
2
3 namespace WS;
4
5 use Ratchet\ConnectionInterface;
6 use Ratchet\MessageComponentInterface;
7
8 class Pusher implements MessageComponentInterface
9 {
10     protected $clients;
11
12     public function __construct()
13     {
14         $this->clients = new \SplObjectStorage;
15     }
16
17     public function broadcast($msg)
18     {
19         foreach ($this->clients as $client)
20         {
21             // The sender is not the receiver, send to each client connected
22             $client->send($msg);
23         }
24     }
25
26     public function onOpen(ConnectionInterface $conn)
27     {
28         echo __METHOD__ . "\n";
29         // Store the new connection to send messages to later
30         $this->clients->attach($conn);
31
32         echo "New connection! ({$conn->resourceId})\n";
33     }
34
35     public function onClose(ConnectionInterface $conn)
36     {
37         echo __METHOD__ . "\n";
38         $this->clients->detach($conn);
39         echo "Connection {$conn->resourceId} has disconnected\n";
40     }
41
42     function onMessage(ConnectionInterface $conn, $msg)
43     {
44         echo __METHOD__ . "\n";
45         echo $msg . PHP_EOL;
46         $conn->send("https://www.youtube.com/watch?v=TR3Vdo5etCQ");
47     }
48
49     public function onError(ConnectionInterface $conn, \Exception $e)
50     {
51         echo __METHOD__ . "\n";
52     }
53 }