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 2cecd5a commit 8b6fde5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Api/WebSocket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected function init(){

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

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

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

Expand Down Expand Up @@ -121,6 +123,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=[];
//print_r($all_sub);die;
//默认返回所有数据
Expand All @@ -129,7 +132,8 @@ protected function getData($global,$callback=null,$sub=[]){
if(is_array($v)) $table=$k;
else $table=$v;

$data=$global->get(strtolower($table));
//$data=$global->get(strtolower($table));
$data=$global_local['public'][strtolower($table)];
if(empty($data)) continue;
$temp[$table]=$data;
}
Expand All @@ -148,8 +152,8 @@ protected function getData($global,$callback=null,$sub=[]){
$data=$global->getQueue(strtolower($v));
}else{
//public

$data=$global->get(strtolower($v));
//$data=$global->get(strtolower($v));
$data=$global_local['public'][strtolower($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 @@ -24,6 +24,7 @@ class SocketServer
private $config=[];

private $public_url=['public','kline'];
private $local_global=['public'=>[],'private'=>[]];

function __construct(array $config=[])
{
Expand Down Expand Up @@ -112,7 +113,8 @@ private function onMessage($global){
if($con->tag=='public'){
if(isset($data['table']) && isset($data['data'][0]['symbol'])) {
$table=strtolower($data['table'].':'.$data['data'][0]['symbol']);
$global->save($table,$data);
//$global->save($table,$data);
$this->local_global['public'][$table]=$data;

/*$debug=$global->get('debug2');
if($debug==1){
Expand Down Expand Up @@ -217,6 +219,11 @@ private function other($con,$global){
//private
}
});

//异步保存数据,不然会有阻塞问题。 0.2秒保存一次
Timer::add(0.2, function() use($global) {
$global->save('global_local',$this->local_global);
});
}

/**
Expand Down

0 comments on commit 8b6fde5

Please sign in to comment.