-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerHandler.php
More file actions
executable file
·68 lines (60 loc) · 1.77 KB
/
Copy pathServerHandler.php
File metadata and controls
executable file
·68 lines (60 loc) · 1.77 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
use WSSC\Contracts\ConnectionContract;
use WSSC\Contracts\WebSocket;
use WSSC\Exceptions\WebSocketException;
class ServerHandler extends WebSocket
{
public $pathParams = [':entity', ':context', ':room'];
private $clients = [];
public function onOpen(ConnectionContract $conn)
{
$this->clients[$this->pathParams[':room']][] = $conn;
$conn->send(json_encode([
"id" => "eb4e0ec3",
"event" => "open",
"room" => $this->pathParams[':room'],
"clients" => count($this->clients[$this->pathParams[':room']])
]));
}
public function onMessage(ConnectionContract $recv, $msg)
{
/** @var ConnectionContract $client */
foreach ($this->clients[$this->pathParams[':room']] as $client) {
$client->send(json_encode([
"id" => "eb4e0ec3",
"content" => $msg
]));
}
}
public function onClose(ConnectionContract $conn)
{
unset($this->clients[array_search($conn, $this->clients)]);
$conn->close();
}
/**
* @param ConnectionContract $conn
* @param WebSocketException $ex
*/
public function onError(ConnectionContract $conn, WebSocketException $ex)
{
echo 'Error occured: ' . $ex->printStack();
}
/**
* You may want to implement these methods to bring ping/pong events
* @param ConnectionContract $conn
* @param string $msg
*/
public function onPing(ConnectionContract $conn, $msg)
{
// TODO: Implement onPing() method.
}
/**
* @param ConnectionContract $conn
* @param $msg
* @return mixed
*/
public function onPong(ConnectionContract $conn, $msg)
{
// TODO: Implement onPong() method.
}
}