-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworker.php
40 lines (30 loc) · 1.05 KB
/
worker.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
<?php
/** @var array - subscribers */
$subscribedTopics = array();
$server = new swoole_websocket_server("0.0.0.0", 9502, SWOOLE_BASE);
$server->on("start", function ($server) {
echo "Swoole http server is started at http://127.0.0.1:9502\n";
});
$server->on('connect', function($server, $req) {
echo "connect: {$req}\n";
});
$server->on('open', function($server, $req) {
echo "connection open: {$req->fd}\n";
});
$server->on('message', function($server, $frame) use (&$subscribedTopics) {
if (
$message = json_decode($frame->data, true)
and isset($message['command'])
and $message['command'] == 'update_data'
and isset($subscribedTopics[$message['user']])
) {
$subscribedTopics[$message['user']]['server']
->push($subscribedTopics[$message['user']]['frame']->fd, $frame->data);
} else {
$subscribedTopics[$frame->data] = ['frame' => $frame, 'server' => $server];
}
});
$server->on('close', function($server, $fd) {
echo "connection close: {$fd}\n";
});
$server->start();