在构建udp服务时,开启daemonize=1,发现存redis没作用。不开启daemonize。就没问题。

来源:4-3 Swoole--UDP服务

宝慕林7068255

2018-04-19

<?php


// require_once 'Predis.php';


class UdpServer{

    

    

    const HOST = '0.0.0.0';

    const PORT = '9502';

    

    public $udp = null;

    const  memberKeyPv = 'data_report_pv';

    const  memberKeyReport = 'data_report_';

    public $redis = '';

    public function __construct(){

        

        //创建Server对象,监听 0.0.0.0:9502端口,类型为SWOOLE_SOCK_UDP

        $this->udp = new swoole_server(self::HOST, self::PORT, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

        $this->udp->set([

            'worker_num' => 8,

            'task_worker_num' => 64,

            'daemonize' => FALSE,

            'daemonize' => 1

            

        ]);

        //监听数据接收事件

        $this->udp->on('Packet', function ($serv, $data, $clientInfo) {

            $this->udp->task($data);

        });

        $this->udp->on("task", [$this, 'onTask']);

        $this->udp->on("finish", [$this, 'onFinish']);

            //启动服务器

        $this->udp->start(); 

    }

    /**

     * @param $serv

     * @param $taskId

     * @param $workerId

     * @param $data

     */

    public function onTask($serv, $taskId, $workerId, $data) {

        

        

        // 分发 task 任务机制,让不同的任务 走不同的逻辑

        $data = json_decode($data,true);

        $method = $data['method'];

        $flag = $this->$method($data, $serv);

    }

    

    public function onFinish($serv,$taskId,$data){

        

        echo "taskId:{$taskId}\n";

        echo "finish-data-sucess:{$data}\n";

        

    }

    

    public function addData($data, $serv){

        

        $config = $this->getConfig($data['ip']);

        

        date_default_timezone_set('Asia/Shanghai');

        $this->redis = new Redis();

        $result = $this->redis->connect($config['hostname'],$config['port'], $config['connectionTimeout']);

        if($result === false) {

            throw new \Exception('redis connect error');

        }

        if(isset($config['password']) && $config['password'] != ''){

            $this->redis->auth($config['password']);

        }

        if($config['database'] != ''){

            $this->redis->select($config['database']);

        }

        $addResult = $this->redis->sAdd(self::memberKeyReport.date('Ymd'),$data['key']);//每日一个pv的key集合

        $result = $this->redis->rpush($data['key'],json_encode($data['data'],true));

       

//         $number = $redis->get($data['pvKey']);

//         if($number == '(nil)'){

//             $number = 1;

//         }

//         $redis->sAdd(self::memberKeyPv.date('Ymd'),$data['pvKey']);//每日一个pv的key集合

//         $redis->set($data['pvKey'],intval($number)+1);

        

        

        

    }

    

    public function getConfig($ip = '172.16.79.20'){


         $config = [

                'hostname' => '127.0.0.1',

                'port' => 6379,

                'database' => 1,

                'connectionTimeout'=> 1,

                'dataTimeout'=> 5,

            ];

        

        return $config;

        

    }

    

    

    

}


$obj = new UdpServer();



写回答

3回答

宝慕林7068255

提问者

2018-04-20

已解决

0
0

宝慕林7068255

提问者

2018-04-19

估计代码是有用的。但是有一个问题。就是我redis,代码sadd进去了。然后我代码lrange拿出来了,并且数据正确。但是我redis-cli进去查看时,却发现并没进来。请问是怎么回事。我在ontask里,使用了new redis()

0
0

宝慕林7068255

提问者

2018-04-19

//img.mukewang.com/szimg/5ad8591f00017d9d06320291.jpg进程是在的。

0
0

Swoole入门到实战打造高性能赛事直播平台

swoole创始人推荐,借助Swoole打造高性能直播平台

2228 学习 · 1062 问题

查看课程