-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyWebSocket.php
36 lines (29 loc) · 950 Bytes
/
MyWebSocket.php
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
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class MyWebSocket implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "Nova conexão! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->clients as $client) {
if ($client !== $from) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
echo "Conexão fechada! ({$conn->resourceId})\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "Erro: {$e->getMessage()}\n";
$conn->close();
}
}