PHP code example of anlity / yii2-swoole-async-timer

1. Go to this page and download the library: Download anlity/yii2-swoole-async-timer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anlity / yii2-swoole-async-timer example snippets


return [
    'swooleAsyncTimer' => [
        'host'             => '127.0.0.1', 		//服务启动IP
        'port'             => '9512',      		//服务启动端口
        'process_name'     => 'swooleServ',		//服务进程名
        'open_tcp_nodelay' => '1',         		//启用open_tcp_nodelay
        'daemonize'        => '1',				//守护进程化
        'worker_num'       => '2',				//work进程数目
        'task_worker_num'  => '2',				//task进程的数量
        'task_max_request' => '10000',			//work进程最大处理的请求数
        'task_tmpdir'      => dirname(__DIR__).'/runtime/task',		 //设置task的数据临时目录
        'log_file'         => dirname(__DIR__).'/runtime/logs/swooleHttp.log', //指定swoole错误日志文件
        'client_timeout'   => '20',				 //client链接服务器时超时时间(s)
        'pidfile'          => '/tmp/y.pid', 		 //服务启动进程id文件保存位置

        //--以上配置项均来自swoole-server的同名配置,可随意参考swoole-server配置说明自主增删--
        'sender_client'    => 'swoole',         //请求服务端的客户端方式(swoole|curl)
                'auth_key'          => 'xxxxxxxxxxxxxxx', //授权密钥
                'max_time_diff'      => 0,              //请求服务端允许的最大时间差
        'debug'            => true,             //是否开启调试模式
        'with_timer'       => true,            //是否使用定时器
        'timer_interval'   => 5000,            //定时器时间间隔
        'log_size'         => 204800000, 			 //运行时日志 单个文件大小
        'log_dir'          => dirname(__DIR__).'/runtime/logs',			 //运行时日志 存放目录
    ]
];

$params = array_merge(
    // ...
    

'controllerMap' => [
    'swoole_server' => [
        'class' => 'anlity\swooleAsyncTimer\SwooleAsyncTimerController',
    ],
],

'components' => [
    'swooleAsyncTimer' => [
        'class' => 'common\components\SwooleAsyncTimer',
    ]
]


namespace common\components;

use anlity\swooleAsyncTimer\SocketInterface;
use anlity\swooleAsyncTimer\SwooleAsyncTimerComponent;

class SwooleAsyncTimer extends SwooleAsyncTimerComponent implements SocketInterface {

    public function timerCallback($timerId, $server){
        // 定时器的回调逻辑
    }

    public function onWorkerStart($server, $workerId){
    }

    public function onWorkerStop($server, $workerId){
    }

    public function onOpen($fd){
        // 与客户端握手时的逻辑,可以把$fd写入到session或者缓存中
    }


    public function onClose($fd){
        // 与客户端断开连接时的逻辑
    }


    public function onMessage($fd, $data){
        // 收到客户端的消息的逻辑
    }
}

//启动
./yii swoole_server/run start
 
//重启
./yii swoole_server/run restart

//停止
./yii swoole_server/run stop

//查看状态
./yii swoole_server/run stats

//查看进程列表
./yii swoole_server/run list



class TestController extends Controller 
{  
	public function actionSwooleasync(){
		$data = [
			"data"=>[
				[
					"a" => "test/mail",
					"p" => ["测试邮件1","测试邮件2"]
				],
				// ...
			],
			"finish" => [
				[
					"a" => "test/mail",
					"p" => ["测试邮件回调1","测试邮件回调2"]
				],
				// ...
			]
		];
		\Yii::$app->SwooleAsyncTimer->async(json_encode($data));
	}

	public function actionMail($a='',$b=''){
		echo $a.'-'.$b;
	}  
}



class TestController extends Controller 
{  
	public function actionPushMsg(){
		$fd = 1;
		$data = [];
		\Yii::$app->SwooleAsyncTimer->pushMsg($fd, $data);
	}

}



class TestController extends Controller 
{  
	public function actionPushMsg(){
		$fd = 1;
		$data = [];
		\Yii::$app->SwooleAsyncTimer->pushMsgByCli($fd, $data);
	}

}

php composer.phar