-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathStatusApplication.php
199 lines (181 loc) · 5.02 KB
/
StatusApplication.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Bloatless\WebSocket\Application;
use Bloatless\WebSocket\Connection;
class StatusApplication extends Application
{
/**
* Holds client connected to the status application.
*
* @var array $clients
*/
private array $clients = [];
/**
* Holds IP/Port information of all clients connected to the server.
*
* @var array $serverClients
*/
private array $serverClients = [];
/**
* Basic server infos (like max. clients e.g.)
*
* @var array $serverInfo
*/
private array $serverInfo = [];
/**
* Total number of connected clients.
*
* @var int $serverClientCount
*/
private int $serverClientCount = 0;
/**
* Handles new connections to the application.
*
* @param Connection $connection
* @return void
*/
public function onConnect(Connection $connection): void
{
$id = $connection->getClientId();
$this->clients[$id] = $connection;
$this->sendServerInfo($connection);
}
/**
* Handles client disconnects from the application.
*
* @param Connection $connection
* @return void
*/
public function onDisconnect(Connection $connection): void
{
$id = $connection->getClientId();
unset($this->clients[$id]);
}
/**
* This application does not expect any incoming client data.
*
* @param string $data
* @param Connection $client
* @return void
*/
public function onData(string $data, Connection $client): void
{
// currently not in use...
}
public function onIPCData(array $data): void
{
// TODO: Implement onIPCData() method.
}
/**
* Sets basic server data.
*
* @param array $serverInfo
* @return bool
*/
public function setServerInfo(array $serverInfo): bool
{
$this->serverInfo = $serverInfo;
return true;
}
/**
* This method is called by the server whenever a client connects (to server).
*
* @param string $ip
* @param int $port
* @return void
*/
public function clientConnected(string $ip, int $port): void
{
$client = $ip . ':' . $port;
$this->serverClients[$client] = true;
$this->serverClientCount++;
$this->statusMsg('Client connected: ' . $client);
$data = [
'client' => $client,
'clientCount' => $this->serverClientCount,
];
$encodedData = $this->encodeData('clientConnected', $data);
$this->sendAll($encodedData);
}
/**
* This method is called by the server whenever a client disconnects (from server).
*
* @param string $ip
* @param int $port
* @return void
*/
public function clientDisconnected(string $ip, int $port): void
{
$client = $ip . ':' . $port;
if (!isset($this->serverClients[$client])) {
return;
}
unset($this->serverClients[$client]);
$this->serverClientCount--;
$this->statusMsg('Client disconnected: ' . $client);
$data = [
'client' => $client,
'clientCount' => $this->serverClientCount,
];
$encodedData = $this->encodeData('clientDisconnected', $data);
$this->sendAll($encodedData);
}
/**
* This method will be called by server whenever there is activity on a port.
*
* @param string $client
* @return void
*/
public function clientActivity(string $client): void
{
$encodedData = $this->encodeData('clientActivity', $client);
$this->sendAll($encodedData);
}
/**
* Sends a status message to all clients connected to the application.
*
* @param string $text
* @param string $type
*/
public function statusMsg(string $text, string $type = 'info'): void
{
$data = [
'type' => $type,
'text' => '[' . date('m-d H:i') . '] ' . $text,
];
$encodedData = $this->encodeData('statusMsg', $data);
$this->sendAll($encodedData);
}
/**
* Sends server information to a client.
*
* @param Connection $client
* @return void
*/
private function sendServerInfo(Connection $client): void
{
if (count($this->clients) < 1) {
return;
}
$currentServerInfo = $this->serverInfo;
$currentServerInfo['clientCount'] = count($this->serverClients);
$currentServerInfo['clients'] = $this->serverClients;
$encodedData = $this->encodeData('serverInfo', $currentServerInfo);
$client->send($encodedData);
}
/**
* Sends data to all clients connected to the application.
*
* @param string $encodedData
* @return void
*/
private function sendAll(string $encodedData): void
{
if (count($this->clients) < 1) {
return;
}
foreach ($this->clients as $client) {
$client->send($encodedData);
}
}
}