public function onConnect($connection) { $connection->timeout_timerid = Timer::add(10, function () use($connection) { echo "timeout\n"; $connection->close(); }, null, false); }
public static function onRemoteClose() { echo "Waring channel connection closed and try to reconnect\n"; self::$_remoteConnection = null; self::clearTimer(); self::$_reconnectTimer = Timer::add(1, 'Channel\\Client::connect', array(self::$_remoteIp, self::$_remotePort)); }
public function scheduleRespawn($delay) { Timer::add($delay / 1000, function ($self) { if ($self->respawnCallback) { call_user_func($self->respawnCallback); } }, array($this), false); }
/** * 当进程启动时一些初始化工作 * @return void */ protected function onWorkerStart() { Timer::add(1, array($this, 'checkGatewayConnections')); $this->checkGatewayConnections(); \GatewayWorker\Lib\Gateway::setBusinessWorker($this); if ($this->_onWorkerStart) { call_user_func($this->_onWorkerStart, $this); } }
/** * 牌局玩家进度广播 */ public static function doApi($player, $data, &$re, $client_id) { $uid = $player->uid; $table = TableDao::getTable($player->tableId); if (!$table) { return 1; } if (!Timer::isExistTimer($table->blinkTimeOut)) { Gateway::bindUid($client_id, $uid); $table->blinkTimeOut = Timer::add(Constants::TABLE_INIT_CHECK_TIME, array($table, 'checkTime')); TableDao::addTable($table->tableId, $table); } if (!isset($table->playerStatus[$uid])) { $table->addUid($uid); GameDao::addInGamePlayer($uid); TableDao::addTable($table->tableId, $table); } if ($data['st'] == 1) { if (!in_array($uid, $table->readyUids)) { $table->readyUids[] = $uid; TableDao::addTable($table->tableId, $table); } if (count($table->readyUids) >= 3) { $table->recordTime = time(); TableDao::addTable($table->tableId, $table); $re['uid'] = -1; Gateway::sendToUid($table->uids, json_encode($re)); } } if ($data['addVal'] != -1) { $uids = $table->uids; $re['uid'] = $uid; foreach ($uids as $_uid) { if ($_uid == $uid) { continue; } $re['addVal'] = $data['addVal']; $re['oldVal'] = $data['oldVal']; Gateway::sendToUid($_uid, json_encode($re)); } } return 1; }
/** * Display a listing of the resource. * * @return Response */ public function index() { require_once '/vendor/workerman/workerman/Autoloader.php'; $http_worker = new Worker("websocket://0.0.0.0:2345"); $http_worker->count = 4; $http_worker->onConnect = function ($connection) { $connection->send('id' . $connection->id); }; $http_worker->onWorkerStart = function ($worker) { // 定时,每10秒一次 \Workerman\Lib\Timer::add(2, function () use($worker) { // 遍历当前进程所有的客户端连接,发送当前服务器的时间 foreach ($worker->connections as $connection) { $connection->send(time()); } }); }; $http_worker->onMessage = function ($connection, $data) { $connection->send('hello world' . $data); }; worker::runAll(); }
public function initRoaming($mob) { Timer::add(0.5, array($this, 'initRoamingCallback')); }
/** * 当与Gateway的连接断开时触发 * @param TcpConnection $connection * @return void */ public function onGatewayClose($connection) { $addr = $connection->remoteAddress; unset($this->gatewayConnections[$addr], $this->_connectingGatewayAddresses[$addr]); if (isset($this->_gatewayAddresses[$addr]) && !isset($this->_waitingConnectGatewayAddresses[$addr])) { Timer::add(1, array($this, 'tryToConnectGateway'), array($addr), false); $this->_waitingConnectGatewayAddresses[$addr] = $addr; } }
public function onRegisterConnectionClose() { Timer::add(1, array($this, 'registerAddress'), null, false); }
<?php use Workerman\Worker; use Workerman\Lib\Timer; // composer autoload include __DIR__ . '/../vendor/autoload.php'; $channel_server = new Channel\Server(); $worker = new Worker(); $worker->onWorkerStart = function () { Channel\Client::on('test event', function ($event_data) { echo 'test event triggered event_data :'; var_dump($event_data); }); Timer::add(5, function () { Channel\Client::publish('test event', 'some data'); }); }; Worker::runAll();
use Workerman\Lib\Timer; // 默认监控Workerman的Applications目录 $monitor_dir = realpath(__DIR__ . '/..'); // worker $worker = new Worker(); // 名称,方便status时候辨别 $worker->name = 'FileMonitor'; // 该进程收到reload信号不执行reload $worker->reloadable = false; // 进程启动后安装定时器 $worker->onWorkerStart = function () { global $monitor_dir; // 只在debug模式下监控文件,守护进程模式不监控 if (!Worker::$daemonize) { // 定时检查被监控目录文件1秒内是否有修改 Timer::add(1, 'check_files_change', array($monitor_dir)); } }; // 检查文件1秒内是否有修改 function check_files_change($monitor_dir) { // 递归遍历目录 $dir_iterator = new RecursiveDirectoryIterator($monitor_dir); $iterator = new RecursiveIteratorIterator($dir_iterator); $time_now = time(); foreach ($iterator as $file) { // 只监控php文件 if (pathinfo($file, PATHINFO_EXTENSION) != 'php') { continue; } // 在最近1秒内有修改
\Workerman\Lib\Timer::add(0.5, function () use($consumer) { if (extension_loaded('sysvmsg')) { // 循环取数据 while (1) { $desiredmsgtype = 1; $msgtype = 0; $message = ''; $maxsize = 65535; // 从队列中获取消息 @see http://php.net/manual/zh/function.msg-receive.php @msg_receive($consumer->queue, $desiredmsgtype, $msgtype, $maxsize, $message, true, MSG_IPC_NOWAIT); if (!$message) { return; } // 假设消息数据为json,格式类似{"class":"class_name", "method":"method_name", "args":[]} $message = json_decode($message, true); // 格式如果是正确的,则尝试执行对应的类方法 if (isset($message['class']) && isset($message['method']) && isset($message['args'])) { // 要调用的类名,加上Consumer命名空间 $class_name = "\\Consumer\\" . $message['class']; // 要调用的方法名 $method = $message['method']; // 调用参数,是个数组 $args = (array) $message['args']; // 类存在则尝试执行 if (class_exists($class_name)) { $class = new $class_name(); $callback = array($class, $method); if (is_callable($callback)) { call_user_func_array($callback, $args); } else { echo "{$class_name}::{$method} not exist\n"; } } else { echo "{$class_name} not exist\n"; } } else { echo "unknow message\n"; } } } });
$sender_io->to($to)->emit('new_msg', @$_POST['content']); // 否则向所有uid推送数据 } else { $sender_io->emit('new_msg', @$_POST['content']); } // http接口返回ok return $http_connection->send('ok'); } return $http_connection->send('fail'); }; // 执行监听 $inner_http_worker->listen(); // 一个定时器,定时向所有uid推送当前uid在线数及在线页面数 Timer::add(1, function () { global $uidConnectionMap, $sender_io, $last_online_count, $last_online_page_count; $online_count_now = count($uidConnectionMap); $online_page_count_now = array_sum($uidConnectionMap); // 只有在客户端在线数变化了才广播,减少不必要的客户端通讯 if ($last_online_count != $online_count_now || $last_online_page_count != $online_page_count_now) { $sender_io->emit('update_online_count', "当前<b>{$online_count_now}</b>人在线,共打开<b>{$online_page_count_now}</b>个页面"); $last_online_count = $online_count_now; $last_online_page_count = $online_page_count_now; } }); }); // 启动一个webserver,用于吐html css js,方便展示 // 这个webserver服务不是必须的,可以将这些html css js文件放到你的项目下用nginx或者apache跑 $web = new WebServer('http://0.0.0.0:2123'); $web->addRoot('localhost', __DIR__ . '/web'); // 运行所有的服务 Worker::runAll();
/** * Stop. * @return void */ public static function stopAll() { self::$_status = self::STATUS_SHUTDOWN; // For master process. if (self::$_masterPid === posix_getpid()) { self::log("Workerman[" . basename(self::$_startFile) . "] Stopping ..."); $worker_pid_array = self::getAllWorkerPids(); // Send stop signal to all child processes. foreach ($worker_pid_array as $worker_pid) { posix_kill($worker_pid, SIGINT); Timer::add(self::KILL_WORKER_TIMER_TIME, 'posix_kill', array($worker_pid, SIGKILL), false); } } else { // Execute exit. foreach (self::$_workers as $worker) { $worker->stop(); } exit(0); } }
$connection->close(); } // onWebSocketConnect 里面$_GET $_SERVER是可用的 // var_dump($_GET, $_SERVER); }; }; */ $webserver = new WebServer('http://0.0.0.0:80'); $webserver->addRoot('120.25.163.9', '/workerman/Applications/huicheng/Web'); $webserver->count = 1; $webserver->onWorkerStart = function ($webserver) { //初始化多客服工号对应的昵称 redisData::Set('HCS1@hc-information', '小汇'); redisData::Set('HCS2@hc-information', '小承'); redisData::Set('HCT1@hc-information', '小信'); redisData::Set('HCT2@hc-information', '小息'); //初始化定时间隔 $time_interval = 7000; //初始化access_token transToWxServer::getaccess_token(); transToWxServer::getjsapi_ticket(); //定时获取access_token \Workerman\Lib\Timer::add($time_interval, function () { transToWxServer::getaccess_token(); transToWxServer::getjsapi_ticket(); }); }; // 如果不是在根目录启动,则运行runAll方法 if (!defined('GLOBAL_START')) { Worker::runAll(); }
public function returnToSpawningPosition($waitDuration) { $delay = $waitDuration ? $waitDuration : 4000; $this->clearTarget(); $this->returnTimeout = Timer::add($delay / 1000, array($this, 'timeoutCallback'), array(), false); }
/** * Websocket handshake. * * @param string $buffer * @param \Workerman\Connection\TcpConnection $connection * @return int */ public static function dealHandshake($buffer, $connection) { $pos = strpos($buffer, "\r\n\r\n"); if ($pos) { // handshake complete $connection->handshakeStep = 2; $handshake_response_length = $pos + 4; // Try to emit onWebSocketConnect callback. if (isset($connection->onWebSocketConnect)) { try { call_user_func($connection->onWebSocketConnect, $connection, substr($buffer, 0, $handshake_response_length)); } catch (\Exception $e) { Worker::log($e); exit(250); } catch (\Error $e) { Worker::log($e); exit(250); } } // Headbeat. if (!empty($connection->websocketPingInterval)) { $connection->websocketPingTimer = Timer::add($connection->websocketPingInterval, function () use($connection) { if (false === $connection->send(pack('H*', '8900'), true)) { Timer::del($connection->websocketPingTimer); $connection->websocketPingTimer = null; } }); } $connection->consumeRecvBuffer($handshake_response_length); if (!empty($connection->tmpWebsocketData)) { $connection->send($connection->tmpWebsocketData, true); $connection->tmpWebsocketData = ''; } if (strlen($buffer) > $handshake_response_length) { return self::input(substr($buffer, $handshake_response_length), $connection); } } return 0; }
<?php use Workerman\Worker; use Workerman\Lib\Timer; // composer autoload include __DIR__ . '/../vendor/autoload.php'; $channel_server = new Channel\Server(); $worker = new Worker(); $worker->onWorkerStart = function () { Channel\Client::$onMessage = function ($channel, $data) { var_dump($channel, $data); }; Channel\Client::subscribe('abc'); Timer::add(5, function () { Channel\Client::publish('abc', array('efg')); }); }; Worker::runAll();
/** * 当客户端连接时触发 * @param int $client_id * @return void */ public static function onConnect($client_id) { if (!isset(self::$redisConnection)) { self::$redisConnection = self::connectRedis(); } // 增加定时器(31s关闭客户端连接) $timeid = \Workerman\Lib\Timer::add(31, function ($client_id) { Gateway::closeClient($client_id); }, array($client_id), false); self::$redisConnection->set($client_id, $timeid); // 连接MYSQL数据库 self::$connectHC = isset(self::$connectHC) ? self::$connectHC : Db::instance('ConnectDb'); // 请求客户端注册 self::connectRegister(); }
/** * 当进程启动时一些初始化工作 * @return void */ protected function onWorkerStart() { if (!class_exists('\\Protocols\\GatewayProtocol')) { class_alias('\\GatewayWorker\\Protocols\\GatewayProtocol', 'Protocols\\GatewayProtocol'); } Timer::add(1, array($this, 'checkGatewayConnections')); $this->checkGatewayConnections(); \GatewayWorker\Lib\Gateway::setBusinessWorker($this); if ($this->_onWorkerStart) { call_user_func($this->_onWorkerStart, $this); } }
/** * 初始化 * 统计目录检查 * 初始化任务 * @see Man\Core.SocketWorker::onStart() */ protected function onStart() { // 初始化目录 umask(0); $statistic_dir = Config::$dataPath . $this->statisticDir; if (!is_dir($statistic_dir)) { mkdir($statistic_dir, 0777, true); } $log_dir = Config::$dataPath . $this->logDir; if (!is_dir($log_dir)) { mkdir($log_dir, 0777, true); } // 定时保存统计数据 Timer::add(self::WRITE_PERIOD_LENGTH, array($this, 'writeStatisticsToDisk')); Timer::add(self::WRITE_PERIOD_LENGTH, array($this, 'writeLogToDisk')); // 定时清理不用的统计数据 Timer::add(self::CLEAR_PERIOD_LENGTH, array($this, 'clearDisk'), array(Config::$dataPath . $this->statisticDir, self::EXPIRED_TIME)); Timer::add(self::CLEAR_PERIOD_LENGTH, array($this, 'clearDisk'), array(Config::$dataPath . $this->logDir, self::EXPIRED_TIME)); }
public static function onRemoteClose() { self::clearTimer(); self::$_timer = Timer::add(1, 'Channel\\Client::connect', array(self::$_remoteIp, self::$_remotePort)); }
/** * 执行关闭流程 * @return void */ public static function stopAll() { self::$_status = self::STATUS_SHUTDOWN; // 主进程部分 if (self::$_masterPid === posix_getpid()) { self::log("Workerman[" . basename(self::$_startFile) . "] Stopping ..."); $worker_pid_array = self::getAllWorkerPids(); // 向所有子进程发送SIGINT信号,表明关闭服务 foreach ($worker_pid_array as $worker_pid) { posix_kill($worker_pid, SIGINT); Timer::add(self::KILL_WORKER_TIMER_TIME, 'posix_kill', array($worker_pid, SIGKILL), false); } } else { // 执行stop逻辑 foreach (self::$_workers as $worker) { $worker->stop(); } exit(0); } }
public function setPingTimeout() { Timer::del($this->pingTimeoutTimer); $this->pingTimeoutTimer = Timer::add($this->server->pingInterval + $this->server->pingTimeout, array($this, 'pingTimeoutCallback'), null, false); }
if (isset($_REQUEST['sendTo']) && !empty($_REQUEST['sendTo'])) { $SendTo = $_REQUEST['sendTo']; } $SendContent = json_encode(array("data" => htmlspecialchars($_REQUEST['sendContent']))); if (!empty($SendTo)) { $senderIO->to($SendTo)->emit('new_msg_receive', $SendContent); } else { $senderIO->emit('new_msg_receive', $SendContent); } // return status return $httpConnection->send(json_encode(array("result" => true))); } return $httpConnection->send(json_encode(array("result" => false))); }; // Start listening $innerHttpWorker->listen(); // Init a timer Timer::add(1, function () { global $uidConnectionMap, $senderIO, $lastOnlineCount, $lastOnlinePageCount; $onlineCountNow = count($uidConnectionMap); $onlinePageCountNow = array_sum($uidConnectionMap); // Send updated data when client counts changes if ($lastOnlineCount != $onlineCountNow || $lastOnlinePageCount != $onlinePageCountNow) { $senderIO->emit("update_online_user_counts", json_encode(array("data" => $onlineCountNow))); $lastOnlineCount = $onlineCountNow; $lastOnlinePageCount = $onlinePageCountNow; } }); }); // Start service Worker::runAll();
} Timer::add(5, 'check_lost'); // 每5秒,检查无响应的评测请求 Timer::add(10, 'check_forgotten'); // 每10秒,在数据库中寻找丢失的请求 p('The server <Tasker> has started.'); }; $worker_tasker->onConnect = function ($connection) { $connection->IP = $connection->getRemoteIp(); $connection->cid = 0; $connection->name = ''; $connection->last_ping = 0; $connection->deadline = Timer::add(5, function () use($connection) { Timer::del($connection->deadline); $connection->deadline = 0; sendMsg($connection, array('action' => 'refuse')); $connection->close(); p("A client timeout logging in. ( IP = {$connection->IP} )"); }); p("A new client has joined. ( IP = {$connection->IP} )"); }; $worker_tasker->onMessage = function ($connection, $data) { $data = json_decode($data, True); if ($connection->IP == '127.0.0.1' && isset($data['pass'], $data['task']) && $data['pass'] == sha1(DB_PASS)) { if (!isset($data['task']['action'])) { $solution = new Solution($data['task']); $solution->push(); return; } else { switch ($data['task']['action']) { case 'kill':
/** * 当Gateway启动的时候触发的回调函数 * @return void */ public function onWorkerStart() { // 分配一个内部通讯端口 $this->lanPort = function_exists('posix_getppid') ? $this->startPort - posix_getppid() + posix_getpid() : $this->startPort; if ($this->lanPort < 0 || $this->lanPort >= 65535) { $this->lanPort = rand($this->startPort, 65535); } // 如果有设置心跳,则定时执行 if ($this->pingInterval > 0) { $timer_interval = $this->pingNotResponseLimit > 0 ? $this->pingInterval / 2 : $this->pingInterval; Timer::add($timer_interval, array($this, 'ping')); } if (!class_exists('\\Protocols\\GatewayProtocol')) { class_alias('\\GatewayWorker\\Protocols\\GatewayProtocol', 'Protocols\\GatewayProtocol'); } // 初始化gateway内部的监听,用于监听worker的连接已经连接上发来的数据 $this->_innerTcpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}"); $this->_innerTcpWorker->listen(); $this->_innerUdpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}"); $this->_innerUdpWorker->transport = 'udp'; $this->_innerUdpWorker->listen(); // 重新设置自动加载根目录 Autoloader::setRootPath($this->_appInitPath); // 设置内部监听的相关回调 $this->_innerTcpWorker->onMessage = array($this, 'onWorkerMessage'); $this->_innerUdpWorker->onMessage = array($this, 'onWorkerMessage'); $this->_innerTcpWorker->onConnect = array($this, 'onWorkerConnect'); $this->_innerTcpWorker->onClose = array($this, 'onWorkerClose'); // 注册gateway的内部通讯地址,worker去连这个地址,以便gateway与worker之间建立起TCP长连接 if (!$this->registerAddress()) { $this->log('registerAddress fail and exit'); Worker::stopAll(); } if ($this->_onWorkerStart) { call_user_func($this->_onWorkerStart, $this); } }
public function run($mapFilePath) { $self = $this; $this->map = new Map($mapFilePath); $this->map->ready(function () use($self) { $self->initZoneGroups(); $self->map->generateCollisionGrid(); // Populate all mob "roaming" areas foreach ($self->map->mobAreas as $a) { $area = new MobArea($a->id, $a->nb, $a->type, $a->x, $a->y, $a->width, $a->height, $self); $area->spawnMobs(); // @todo bind //$area->onEmpty($self->handleEmptyMobArea->bind($self, area)); $area->onEmpty(function () use($self, $area) { call_user_func(array($self, 'handleEmptyMobArea'), $area); }); $self->mobAreas[] = $area; } // Create all chest areas foreach ($self->map->chestAreas as $a) { $area = new ChestArea($a->id, $a->x, $a->y, $a->w, $a->h, $a->tx, $a->ty, $a->i, $self); $self->chestAreas[] = $area; // @todo bind $area->onEmpty(function () use($self, $area) { call_user_func(array($self, 'handleEmptyChestArea'), $area); }); } // Spawn static chests foreach ($self->map->staticChests as $chest) { $c = $self->createChest($chest->x, $chest->y, $chest->i); $self->addStaticItem($c); } // Spawn static entities $self->spawnStaticEntities(); // Set maximum number of entities contained in each chest area foreach ($self->chestAreas as $area) { $area->setNumberOfEntities(count($area->entities)); } }); $this->map->initMap(); $regenCount = $this->ups * 2; $updateCount = 0; Timer::add(1 / $this->ups, function () use($self, $regenCount, &$updateCount) { $self->processGroups(); $self->processQueues(); if ($updateCount < $regenCount) { $updateCount += 1; } else { if ($self->regenCallback) { call_user_func($self->regenCallback); } $updateCount = 0; } }); echo $this->id . " created capacity: " . $this->maxPlayers . " players \n"; }
/** * 定时检查子进程是否退出了 */ protected static function monitorWorkers() { // 定时检查子进程是否退出了 Timer::add(0.5, "\\Workerman\\Worker::checkWorkerStatus"); // 主进程loop self::$globalEvent->loop(); }
public function resetTimeout() { Timer::del($this->disconnectTimeout); // 15分钟 $this->disconnectTimeout = Timer::add(15 * 60, array($this, 'timeout'), false); }