private function online($who) { if (!isset($this->gens[$who])) { $this->gens[$who] = $this->messageGenerator($who); $this->sendUsers(); $this->broadcast($who, $who . " is online."); } if ($this->timer == null) { $this->timer = swoole_timer_add(1000, function () { $users = $this->getAllUsers(); foreach ($users as $user) { if (!isset($this->getMessage[$user]) && !isset($this->getUpdateUsers[$user])) { if (!isset($this->maybeOffline[$user])) { $this->maybeOffline[$user] = true; } else { $this->offline($user); } } else { if (isset($this->maybeOffline[$user])) { unset($this->maybeOffline[$user]); } } } }); } }
public function tick($worker) { $this->worker = $worker; swoole_timer_add(500, function () { while (true) { $this->checkExit(); $task = $this->getQueue(); if (empty($task)) { break; } $this->Run($task); } }); }
public static function addTimeOut($socket) { if (!self::$timerEnable) { self::$timerEnable = true; swoole_timer_add(100, function ($interval) { foreach (self::$timerSockets as $k => $v) { $timeNow = microtime(true); if ($timeNow - $v->createTime > $v->timeout) { @$v->close(); unset(self::$timerSockets[$k]); } } }); } self::$timerSockets[$socket->sock] = $socket; }
<?php date_default_timezone_set('Asia/Shanghai'); swoole_timer_add(3000, function ($interval) { echo "b timer[{$interval}] :" . date("H:i:s") . " call\n"; }); swoole_timer_add(1000, function ($interval) { echo "a timer[{$interval}] :" . date("H:i:s") . " call\n"; });
global $i; echo "#okcoin " . $i++ . "\n"; $data_obj = new \TradeData\OkcoinTradeData(); $data = $data_obj->data; echo $data . "\n"; $data_obj->writeData(); }); swoole_timer_add(2003, function ($interval) { global $i; echo "#chbtc " . $i++ . "\n"; $data_obj = new \TradeData\ChbtcTradeData(); $data = $data_obj->data; echo $data . "\n"; $data_obj->writeData(); }); swoole_timer_add(2004, function ($interval) { global $i; echo "#bitstamp " . $i++ . "\n"; $data_obj = new \TradeData\BitstampTradeData(); $data = $data_obj->data; echo $data . "\n"; $data_obj->writeData(); }); swoole_timer_add(2005, function ($interval) { global $i; echo "#btce " . $i++ . "\n"; $data_obj = new \TradeData\BtceTradeData(); $data = $data_obj->data; echo $data . "\n"; $data_obj->writeData(); });
<?php swoole_timer_add(3000, function ($interval) { echo "timer[{$interval}] :" . microtime(true) . " called\n"; swoole_timer_del(2000); }); swoole_timer_add(1000, function ($interval) { echo "timer[{$interval}] :" . microtime(true) . " called\n"; swoole_timer_del(1000); swoole_timer_del(1000); }); swoole_timer_add(2000, function ($interval) { echo "timer[{$interval}] :" . microtime(true) . " called\n"; }); swoole_process::signal(SIGTERM, function () { swoole_event_exit(); });
<?php swoole_timer_add(1000, function ($interval) { echo "timer[{$interval}] :" . microtime(true) . " called\n"; }); echo "Added timer1: " . microtime(true) . "\n"; swoole_timer_add(3000, function ($interval) { echo "timer[{$interval}] :" . microtime(true) . " called\n"; static $remove = false; if (!$remove) { swoole_timer_after(10000, function () { echo microtime(true) . " Timeout, clear interval\n"; swoole_timer_del(3000); }); $remove = true; } }); echo "Added timer2: " . microtime(true) . "\n";
{ $binpath = $_SERVER["_"]; $worker->exec($binpath, array(ROOT_PATH . "http_test.php", $worker->pipe)); } $process = new swoole_process("fork"); $process->start(); function process_run($worker) { echo "abc\n"; $worker->exit(0); exit; } function create_process() { $process = new swoole_process("process_run"); $process->start(); } swoole_timer_add(1000, function ($interval) { create_process(); create_process(); }); swoole_process::signal(SIGCHLD, function ($signo) { while ($pid = pcntl_wait($status, WNOHANG)) { echo $pid . "\n"; } }); swoole_event_add($process->pipe, function ($pipe) use($process) { $ret = $process->read(); echo $ret; $process->write($ret); });
<?php $cli = new swoole_client(SWOOLE_TCP | SWOOLE_PACKET, SWOOLE_SOCK_ASYNC); $cli->on("connect", function (swoole_client $cli) { $cli->send("async client connected"); swoole_timer_add(1000, function () use($cli) { $cli->send("hello world"); }); }); $cli->on("receive", function ($cli, $data) { echo "receive:{$data},len:" . strlen($data) . "\r\n"; }); $cli->on("close", function ($cli) { $cli->close(); }); $cli->on("error", function ($cli) { exit("connect failed. Error:{$cli->errCode}\r\n"); }); $cli->connect("127.0.0.1", 5900, -1);
case Chatroom::TYPE: break; } }); swoole_timer_add(30000, function ($interval) use($client, $infiniteCounterHandler) { if ($client->isAuthorized()) { //send online user chat packet $uidList = UserManager::getInstance()->getAllOnlineUsers(); if (count($uidList) > 1) { $chat = new TextChat(); $chat->from = COUNTER_UID; foreach ($uidList as $uid) { if ($uid != COUNTER_UID) { $chat->to = $uid; $chat->setText($infiniteCounterHandler->increase()); $client->sendPacket($chat); } } } /*// send groupchat $groupchat = new TextGroupChat(); $groupchat->from = COUNTER_UID; $groupchat->setSendToAll(); $groupchat->setGroupId(COUNTER_GROUP_ID); $groupchat->setText($infiniteCounterHandler->increase()); $client->sendPacket($groupchat);*/ } }); $client->connect(); class InfiniteCounter { protected $number = 0;
<?php ini_set('date.timezone', 'Asia/Shanghai'); define('ROOTDIRECTORY_PATH', dirname(dirname(__FILE__))); require 'classes/base.class.php'; require 'classes/config.class.php'; require 'classes/handle.class.php'; require 'classes/redis.class.php'; require 'classes/tasks.class.php'; require ROOTDIRECTORY_PATH . '/vendor/autoload.php'; swoole_timer_add(500, function () { try { callback_timer_function(); } catch (Exception $e) { file_put_contents('/tmp/crontab_error.log', '[' . date('H:i:s') . '] =>' . json_encode($e) . "\r\n", FILE_APPEND); } }); function callback_timer_function() { $Handle = new TimingCall_Tasks(); $Handle->upDaemonTime(); $tasks = $Handle->readExecuteTasks(); foreach ($tasks as $task) { $process = new swoole_process('callback_process_function', true); $process->write(json_encode($task)); $process->start(); $process->wait(false); } } function callback_process_function(swoole_process $worker) {
/** * 注册定时任务 */ protected static function register_timer() { swoole_timer_add(60000, function ($interval) { Crontab::load_config(); }); swoole_timer_add(1000, function ($interval) { Crontab::do_something($interval); }); }
/** * 注册信号 */ private static function register_signal() { //注册子进程退出信号逻辑 swoole_process::signal(SIGCHLD, function ($signo) { while (($pid = pcntl_wait($status, WNOHANG)) > 0) { Main::log_write("收到子进程{$pid}退出信号"); if (!isset(Squire_Master::$workers[$pid]["logout"])) { $task = Squire_Master::$workers[$pid]["task"]; Squire_Master::create_child_process($task, Squire_Master::$task_list[$task]); } unset(Squire_Master::$workers[$pid]); } }); //注册主进程退出逻辑 swoole_process::signal(SIGTERM, function ($signo) { Main::log_write("收到主进程退出信号, 发送子进程退出信号:" . $signo); foreach (Squire_Master::$workers as $pid => $process) { Squire_Master::$workers[$pid]["logout"] = true; swoole_process::kill($pid, SIGUSR2); } if (!empty(Main::$http_server)) { swoole_process::kill(Main::$http_server->pid, SIGKILL); } Main::log_write("已发送子进程退出信号,主进程正在退出....."); swoole_timer_add(501, function () { if (count(Squire_Master::$workers) == 0) { Squire_Master::exit2p("主进程退出成功"); } }); }); //注册重新载入配置信号 swoole_process::signal(SIGUSR1, function ($signo) { Main::log_write("收到重新载入配置信号:" . $signo); Squire_Master::reload(); }); }