Skip to content

Commit

Permalink
solve the websocket blocking problem
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Jun 18, 2021
1 parent 85b05c4 commit 24b890f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Api/WebSocket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ protected function init(){

$this->add('keysecret',[]);//目前总共key

$this->add('global_local',[]);//临时存储数据

$this->add('debug',[]);
}

Expand Down Expand Up @@ -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=[];

//默认返回所有数据
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/Api/WebSocket/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SocketServer
private $connection=[];
private $connectionIndex=0;
private $config=[];
private $local_global=['public'=>[],'private'=>[]];

function __construct(array $config=[])
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
}

/**
Expand Down

0 comments on commit 24b890f

Please sign in to comment.