(new \swoole_process(function ($process) use ($serv) {
cli_set_process_title("websocket_async_redis_subscribe_process");
$thisdb = Model::init("syncredisdb");
$thisredis=TbRedis::init("wbscktinit");
$redis = new \swoole_redis(array('password' => config('redis')['auth']));
//异步redis,处理客户端按需订阅频道
$redis->on('message', function ($redis, $result) use ($serv,$thisdb){
if ($result[1] == 'channel') {
echo " start subscribe:" . $result[2] ;
$chandate=json_decode($result[2],true);
echo " :" . $chandate['cname'] . PHP_EOL;
$redis->subscribe($chandate['cname']);
} elseif ($result[1] == 'channelclose'){
echo " close subscribe:" . $result[2] . PHP_EOL;
$chandate=json_decode($result[2],true);
$redis->unsubscribe($chandate['cname']);
} else {
$data = json_decode($result[2], true);
if (is_array($data) && isset($data['devid']) ){
foreach ($this->client as $row) {
if($serv->exist($row['fd']) && $row['stop']=='n') {
if ($row['deviceID']==$data['tbmac'] && $result[1]==$row['channel'] ) {
$pushdata=array(
'find'=>$data['find'],
……
'time'=>date('Y-m-d H:i:s', $data['time']),
);
$serv->push($row['fd'], json_encode(array('type' => 'Data', 'data' => $pushdata)));//广播
}
}
}
}
}
});
$redis->connect(config('redis')['host'], config('redis')['port'], function ($client, $result) {
$client->subscribe('channel');
$client->subscribe('channelclose');
});
}, false))->start();
//专用订阅频道
$data['cname']="base_initdata_006" ;
$this->client->set($frame->fd, array('channel' => $data['cname'],'stop' => 'n'));
$this->redis->publish('channel', json_encode($data));