-
Notifications
You must be signed in to change notification settings - Fork 24
/
Chat3WebsocketDaemonHandler.php
50 lines (39 loc) · 1.78 KB
/
Chat3WebsocketDaemonHandler.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
<?php
namespace morozovsk\websocket\examples\chat3\server;
class Chat3WebsocketDaemonHandler extends \morozovsk\websocket\Daemon
{
public $userIds = [];
protected function onOpen($connectionId, $info) {//it is called when the connection is open
$message = 'пользователь #' . $connectionId . ' : ' . var_export($info, true) . ' ' . stream_socket_get_name($this->clients[$connectionId], true);
foreach ($this->clients as $clientId => $client) {
$this->sendToClient($clientId, $message);
}
$info['GET'];//or use $info['Cookie'] for use PHPSESSID or $info['X-Real-IP'] if you use proxy-server like nginx
parse_str(substr($info['GET'], 1), $_GET);//parse get-query
//var_export($_GET['id']);
$this->userIds[$connectionId] = $_GET['userId'];
}
protected function onClose($connectionId) {//it is called when existed connection is closed
unset($this->userIds[$connectionId]);
}
protected function onMessage($connectionId, $data, $type) {//it is called when received a message from client
if (!strlen($data)) {
return;
}
$message = 'пользователь #' . $connectionId . ' : ' . strip_tags($data);
foreach ($this->clients as $clientId => $client) {
$this->sendToClient($clientId, $message);
}
}
protected function onServiceMessage($connectionId, $data) {
$data = json_decode($data);
foreach ($this->userIds as $clientId => $userId) {
if ($data->userId == $userId) {
$this->sendToClient($clientId, $data->message);
}
}
/*if (isset($this->clients[$data->clientId])) {
$this->sendToClient($data->clientId, $data->message);
}*/
}
}