-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfunc.php
68 lines (63 loc) · 1.85 KB
/
func.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
<?php
function save_cb($redis, $hash, $content){
if(!$redis || !$hash)return;
$content = htmlspecialchars($content);
$exceptCb = [
'8d3c0804d35128b9546b526245bb9b64',
];
if(in_array($hash, $exceptCb)){
$redis->lPush($hash, $content);
} else {
if($redis->lSize($hash)<50){
$redis->lPush($hash, $content);
} else {
$redis->rPop($hash);
$redis->lPush($hash, $content);
}
}
$redis->bgsave();
}
function publish($redis, $hash, $ws, $content, $raw = false){
if(!$hash || !$redis || !$ws)return;
if ($raw == false) {
$content = htmlspecialchars($content);
}
$result = $redis->lRange('publish_'.$hash, 0, -1);
if ($result) {
krsort($result);
} else {
$result = [];
}
foreach ($result as $k => $v) {
try {
if (!$ws->isEstablished($v)) {
var_dump('cannot find client: '.$v);
// remove client from the push channel.
$redis->lRem('publish_'.$hash, $v, 0);
continue;
}
if ($raw == true) {
$ws->push($v, json_encode($content));
} else {
$tmp = array('type'=>'single', 'data'=>$content);
$ws->push($v, json_encode($tmp));
}
} catch (\Exception $e) {
var_dump('lost client: '.$v, $e->getMessage());
}
}
}
function hash_clear($redis){
if(!$redis)return;
$allkeys = $redis->hKeys('fd.to.hash');
$all = $redis->hGetAll('fd.to.hash');
// remove all login client from push channel
foreach ($all as $fd => $hash) {
$redis->lRem('publish_'.$hash, $fd, 0);
}
// remove all login info
foreach ($allkeys as $k => $v) {
$redis->hDel('fd.to.hash', $v);
}
return;
}