swoole 类中使用定时器

swoole 类中使用定时器  服务端 server


<?php
class TimerServer
{
        private $serv;
        public function __construct() {
                $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set(array(
            'worker_num' => 8,
            'daemonize' => false,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode'=> 1 ,
        ));
        $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
        $this->serv->on('Connect', array($this, 'onConnect'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));
                // bind callback
       // $this->serv->on('Timer', array($this, 'onTimer'));
        $this->serv->start();
        }
        public function onWorkerStart( $serv , $worker_id) {
                // 在Worker进程开启时绑定定时器
        echo "onWorkerStart\n";
        // 只有当worker_id为0时才添加定时器,避免重复添加
        if( $worker_id == 0 ) {

            swoole_timer_after(2500, function () {
                 echo "after 2500ms.\n";
            });

            swoole_timer_tick(1500, function () {
                echo "tick  1500ms.\n";
             });



        }
    }
    public function onConnect( $serv, $fd, $from_id ) {
        echo "Client {$fd} connect\n";
    }
    public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
        echo "Get Message From Client {$fd}:{$data}\n";
    }
    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }

/*
    public function onTimer($serv, $interval) {
        switch( $interval ) {
                case 500: {     //
                        echo "Do Thing A at interval 500\n";
                        break;
                }
                case 1000:{
                        echo "Do Thing B at interval 1000\n";
                        break;
                }
                case 100:{
                        echo "Do Thing C at interval 100\n";
                        break;
                }
        }
    }
*/
}
new TimerServer();

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值