1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSSCTEST; |
4
|
|
|
|
5
|
|
|
use WSSC\Contracts\ConnectionContract; |
6
|
|
|
use WSSC\Contracts\WebSocket; |
7
|
|
|
use WSSC\Exceptions\WebSocketException; |
8
|
|
|
|
9
|
|
|
class ServerHandler extends WebSocket |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/* |
13
|
|
|
* if You need to parse URI context like /messanger/chat/JKN324jn4213 |
14
|
|
|
* You can do so by placing URI parts into an array - $pathParams, when Socket will receive a connection |
15
|
|
|
* this variable will be appropriately set to key => value pairs, ex.: ':context' => 'chat' |
16
|
|
|
* Otherwise leave $pathParams as an empty array |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
public $pathParams = [':entity', ':context', ':token']; |
20
|
|
|
private $clients = []; |
21
|
|
|
|
22
|
|
|
public function onOpen(ConnectionContract $conn) |
23
|
|
|
{ |
24
|
|
|
$this->clients[] = $conn; |
25
|
|
|
echo 'Connection opend, total clients: ' . count($this->clients) . PHP_EOL; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function onMessage(ConnectionContract $recv, $msg) |
29
|
|
|
{ |
30
|
|
|
echo 'Received message: ' . $msg . PHP_EOL; |
31
|
|
|
$recv->send($msg); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function onClose(ConnectionContract $conn) |
35
|
|
|
{ |
36
|
|
|
unset($this->clients[array_search($conn, $this->clients)]); |
37
|
|
|
$conn->close(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ConnectionContract $conn |
42
|
|
|
* @param WebSocketException $ex |
43
|
|
|
*/ |
44
|
|
|
public function onError(ConnectionContract $conn, WebSocketException $ex) |
45
|
|
|
{ |
46
|
|
|
echo 'Error occured: ' . $ex->printStack(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* You may want to implement these methods to bring ping/pong events |
51
|
|
|
* @param ConnectionContract $conn |
52
|
|
|
* @param string $msg |
53
|
|
|
*/ |
54
|
|
|
public function onPing(ConnectionContract $conn, $msg) |
55
|
|
|
{ |
56
|
|
|
// TODO: Implement onPing() method. |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ConnectionContract $conn |
61
|
|
|
* @param $msg |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function onPong(ConnectionContract $conn, $msg) |
65
|
|
|
{ |
66
|
|
|
// TODO: Implement onPong() method. |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|