Fibinger Ádám
2020-06-04 ffa7c6f5c007dfc826aef0ca27d23bb69bbeed7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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";
    }
}