| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 | <?phpuse Workerman\Worker;require_once __DIR__ . '/Autoloader.php';require_once './Lib/mysql/src/Connection.php';use \Workerman\Lib\Timer;$ws_worker = new Worker("tcp://0.0.0.0:8900");// 启动1个进程对外提供服务$ws_worker->count = 1;$ws_worker->onWorkerStart = function($ws_worker){    global $cmdList;    global $resList;    $cmdList = array();    $resList = array();    // 指令返回监测    Timer::add(0.1, function()use($ws_worker){        global $cmdList;        global $resList;        $cmdTemp = $cmdList;        $resTemp = $resList;        if (!empty($resTemp)) {            foreach ($resTemp as $key => $value) {                if (isset($cmdTemp[$value['deviceId']])) {                    $cmdTemp[$value['deviceId']]['client']->send('{"res":0,"info":"success","resInfo":"'.$value['resInfo'].'"}');                    unset($cmdList[$value['deviceId']]);                }                unset($resList[$key]);            }        }    });    // 超时监测    Timer::add(1, function()use($ws_worker){        global $cmdList;        $cmdTemp = $cmdList;        if (!empty($cmdTemp)) {            foreach ($cmdTemp as $key => $value) {                if (time() - $value['time'] >= 30){                    if (isset($value['client'])) {                        unset($cmdList[$key]);                    }                }            }        }    });};$ws_worker->onMessage = function($connection, $data){    global $cmdList;    global $resList;    var_dump($data);    $data = json_decode($data,true);    if ($data) {        if ($data['type'] == 'cmd') {            if (isset($data['deviceId']) && !empty($data['deviceId'])) {                $cmdList[$data['deviceId']] = array('client'=>$connection,'time'=>time());            }        }elseif ($data['type'] == 'res'){            if (isset($data['deviceId']) && !empty($data['deviceId'])) {                $resList[$data['deviceId']] = array('deviceId'=>$data['deviceId'],'resInfo'=>isset($data['resInfo']) ? $data['resInfo'] : '');            }        }    }};// 运行workerWorker::runAll();
 |