commit | author | age
|
39aea3
|
1 |
<?php |
FÁ |
2 |
|
|
3 |
namespace WS; |
|
4 |
|
|
5 |
use Ratchet\MessageComponentInterface; |
|
6 |
use Ratchet\ConnectionInterface; |
|
7 |
|
|
8 |
class Chat implements MessageComponentInterface |
|
9 |
{ |
|
10 |
protected $clients; |
|
11 |
|
|
12 |
public function __construct() |
|
13 |
{ |
|
14 |
$this->clients = new \SplObjectStorage; |
|
15 |
} |
|
16 |
|
|
17 |
public function onOpen(ConnectionInterface $conn) |
|
18 |
{ |
|
19 |
$this->clients->attach($conn); |
|
20 |
$conn->send("Hello overlay client!"); |
|
21 |
} |
|
22 |
|
|
23 |
public function onMessage(ConnectionInterface $conn, $msg) |
|
24 |
{ |
|
25 |
$conn->send("Never talk to me like that again!"); |
|
26 |
} |
|
27 |
|
|
28 |
public function onClose(ConnectionInterface $conn) |
|
29 |
{ |
|
30 |
$this->clients->detach($conn); |
|
31 |
echo "Connection {$conn->resourceId} has disconnected\n"; |
|
32 |
} |
|
33 |
|
|
34 |
public function onError(ConnectionInterface $conn, \Exception $e) |
|
35 |
{ |
|
36 |
echo "An error has occurred: {$e->getMessage()}\n"; |
|
37 |
|
|
38 |
$conn->close(); |
|
39 |
} |
|
40 |
|
|
41 |
public function sendToClients($message) |
|
42 |
{ |
|
43 |
foreach ($this->clients as $client) |
|
44 |
{ |
|
45 |
$client->send($message); |
|
46 |
} |
|
47 |
} |
|
48 |
} |