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'] : ''); } } } }; // 运行worker Worker::runAll();