From 24b890fa8ab3d03f034f4977707717c745c69241 Mon Sep 17 00:00:00 2001 From: lin <465382251@qq.com> Date: Fri, 18 Jun 2021 11:32:42 +0800 Subject: [PATCH] solve the websocket blocking problem --- src/Api/WebSocket/SocketClient.php | 9 +++++++-- src/Api/WebSocket/SocketServer.php | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Api/WebSocket/SocketClient.php b/src/Api/WebSocket/SocketClient.php index c4c8400..ebcb566 100644 --- a/src/Api/WebSocket/SocketClient.php +++ b/src/Api/WebSocket/SocketClient.php @@ -43,6 +43,8 @@ protected function init(){ $this->add('keysecret',[]);//目前总共key + $this->add('global_local',[]);//临时存储数据 + $this->add('debug',[]); } @@ -131,6 +133,7 @@ protected function getData($global,$callback=null,$sub=[]){ $all_sub=$global->get('all_sub'); if(empty($all_sub)) return []; + $global_local=$global->get('global_local'); $temp=[]; //默认返回所有数据 @@ -144,7 +147,8 @@ protected function getData($global,$callback=null,$sub=[]){ $temp[$vv]=$data; } }else{ - $data=$global->get($v); + //$data=$global->get($v); + $data=$global_local['public'][$v]; $temp[$v]=$data; } } @@ -164,7 +168,8 @@ protected function getData($global,$callback=null,$sub=[]){ $data=$global->getQueue($v); }else{ //public - $data=$global->get($v); + //$data=$global->get($v); + $data=$global_local['public'][$v]; } if(empty($data)) continue; diff --git a/src/Api/WebSocket/SocketServer.php b/src/Api/WebSocket/SocketServer.php index cb974db..32a2713 100644 --- a/src/Api/WebSocket/SocketServer.php +++ b/src/Api/WebSocket/SocketServer.php @@ -21,6 +21,7 @@ class SocketServer private $connection=[]; private $connectionIndex=0; private $config=[]; + private $local_global=['public'=>[],'private'=>[]]; function __construct(array $config=[]) { @@ -109,7 +110,8 @@ private function onMessage($global){ }*/ $table=$data['stream']; - $global->save($table,$data); + //$global->save($table,$data); + $this->local_global['public'][$table]=$data; //最后数据更新时间 $con->tag_data_time=time(); @@ -218,6 +220,11 @@ private function other($con,$global){ $this->log('listen '.$con->tag); }); + + //异步保存数据,不然会有阻塞问题。 0.2秒保存一次 + Timer::add(0.2, function() use($global) { + $global->save('global_local',$this->local_global); + }); } /**