swoole添加定时器定时任务的两种方式。
第一种方式:
$ws_server = new swoole_websocket_server($socket_config['host'], $socket_config['port']);
$process = new \swoole_process(function () {
swoole_timer_tick(200000, function () {
//echo time() . "\r\n";
});
});
$ws_server->addProcess($process);
$ws_server->start();
第二种方式:
$ws_server = new swoole_websocket_server($socket_config['host'], $socket_config['port']);
$ws_server->on('WorkerStart', function ($ws, $worker_id) {
// 仅在第一个 Worker 进程启动时启动 Timer 定时器
if ($worker_id == 0) {
// 启动 Timer,每 1000 毫秒回调一次 onTick 函数,
swoole_timer_tick(2000, function () {
//echo time() . "\r\n";
});
}
});
$ws_server->start();