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
<?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);
        }
    }
}