需求:
實(shí)時檢查房間人數(shù),在房間人數(shù)到達(dá)要求后,由服務(wù)器主動向房間推送消息。
問題:
如何實(shí)現(xiàn)GatewayWorker主動向房間推送呢?不是由onMessage回調(diào)去觸發(fā)的。
我的想法:
想用GatewayWorker單開一個進(jìn)程去檢查所有房間的人數(shù),在房間達(dá)到一定人數(shù)后由這個進(jìn)程主動向這個房間去推送數(shù)據(jù),這個具體該怎么實(shí)現(xiàn)呢?
use Workerman\Worker;
use Workerman\Lib\Timer;
use GatewayWorker\Lib\Gateway;
// 單獨(dú)啟動一個worker進(jìn)程運(yùn)行全局定時器
$task = new Worker();
$task->count = 1;
$task->onWorkerStart = function(){
// 設(shè)置成start_register.php里面的端口,這里假設(shè)是1238
Gateway::$registerAddress = '127.0.0.1:1238';
$room_id = xxx;
// 每秒檢查一次$room_id房間人數(shù)
Timer::add(1, function(){
$online_count = Gateway:: getClientCountByGroup($room_id);
// 人數(shù)大于某個值,則發(fā)送消息給這個房間
if($online_count > XX) {
Gateway::sendToGroup($room_id, xxxxx);
}
});
};
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}