Gateway游戲項目,不同房間游戲開始之前定時30秒,30秒后開始處理游戲,上次的問題雖然搞定了異步進行,現(xiàn)在問題是前端顯示會跳過秒數(shù),比如30 29 直接27了這樣,這個秒數(shù)我是從數(shù)據(jù)庫讀出來每一秒都返回給前端,會不會是這個頻繁操作數(shù)據(jù)庫引起的?如果客戶端開多了房間,我估計很快就卡住了,看了社區(qū)的問題想單獨開啟個進程來處理定時器,麻煩指導(dǎo)下該怎么做,新手, 謝謝了。
下面是別人問題中的 http://wenda.workerman.net/?/question/1020
Events.php
public static function onConnect($client_id)
{
$task_connection = new AsyncTcpConnection('Text://127.0.0.1:13000');
$task_connection->send('send to task workerman');
$task_data = array(
'function' => 'send_mail',
'args' => array('from'=>'xxx', 'to'=>'xxx', 'contents'=>'xxx'),
);
$task_connection->send(json_encode($task_data));
$task_connection->onMessage = function($task_connection, $task_result){
// 結(jié)果
var_dump($task_result);
// 獲得結(jié)果后記得關(guān)閉鏈接
$task_connection->close();
};
// 執(zhí)行異步鏈接
$task_connection->connect();
}
然后這段代碼該放在哪里呢
$task_worker = new Worker('Text://127.0.0.1:13000');
$task_worker->count = 10;
$task_worker->name = 'TaskWorker';
$task_worker->onMessage = function($connection, $task_data){
$task_data = json_decode($task_data, true);
$task_result = 'fuck you';
$connection->send(json_encode($task_result));
};
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}