public static function onWorkerStart(Worker $businessWorker)
{
// use think\worker\Application;
// $app = new Application;
// $app->initialize();// 内部通道发起倒计时
$http_worker = new Worker('http://127.0.0.1:8585');
$http_worker->reusePort = true;
$http_worker->onMessage = function (TcpConnection $connection, Request $request) {
$data = $request->get('data');
$data = json_decode($data, true);
$count = (int) $data['time'];
$userid = (int) $data['userid'];
$roomid = (int) $data['roomid'];
$gameid = (int) $data['gameid'];
$sessionid = 'room' . $roomid . 'game' . $gameid . 'userid' . $userid;
if ($count > 0) {
$_SESSION[$sessionid] = Timer::add(1, function () use (&$count, &$userid, &$roomid, &$gameid, &$sessionid) {
Gateway::sendToGroup('room' . $roomid . 'game' . $gameid, json_encode(array(
'type' => 'countdown',
'requestuid' => $userid,
'roomid' => $roomid,
'gameid' => $gameid,
'time' => $count,
)));
$count--;
if ($count < 1) {
Timer::del($_SESSION[$sessionid]);
}
});
return $connection->send('ok');
} else {
var_dump($connection);
// Timer::del($_SESSION[$sessionid]);
}
};
$http_worker->listen();
}
只开了一个进程。要实现MVC代码GET请求http://127.0.0.1:8585/。根据时间time如果为0就删除上一个计数器。但是删除不了。执行后感觉是个死循环,最后就502了。
哪位大神能帮助一下。困扰新手几天了
不要用session 用redis
好的,我试试,谢谢
你的写法有问题,下个请求就把
$_SESSION[$sessionid]
覆盖了,定时器就无法删除了定时器执行一定次数后删除自己文档中有啊 定时器回调中删除当前定时器 搞几天了咋不知道去看眼文档,简直了