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 f1ec28c commit f991d43
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/Api/WebSocketV3/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ protected function init(){

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

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

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

Expand Down Expand Up @@ -117,6 +119,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 @@ -129,8 +132,10 @@ protected function getData($global,$callback=null,$sub=[]){
$data=$global->getQueue(strtolower($table));
$temp[strtolower($table)]=$data;
}else{
$data=$global->get(strtolower($v));
$temp[strtolower($v)]=$data;
//$data=$global->get(strtolower($v));
//$temp[strtolower($v)]=$data;
$k_strtolower=strtolower($v);
$temp[$k_strtolower]=$global_local['public'][$k_strtolower];
}
}
}else{
Expand All @@ -145,7 +150,8 @@ protected function getData($global,$callback=null,$sub=[]){
foreach ($sub as $k=>$v){
if(count($v)==1) {
$table=$v[0];
$data=$global->get(strtolower($table));
//$data=$global->get(strtolower($table));
$data=$global_local['public'][strtolower($table)];
} else {
//判断私有数据是否需要走队列数据
//$temp_v=explode(self::$USER_DELIMITER,$v);
Expand Down
10 changes: 9 additions & 1 deletion src/Api/WebSocketV3/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class SocketServer
private $connectionIndex=0;
private $config=[];

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

function __construct(array $config=[])
{
$this->config=$config;
Expand Down Expand Up @@ -110,7 +112,8 @@ private function onMessage($global){
$table=$this->userKey($con->tag_keysecret,$table);
$global->saveQueue($table,$data);
}else{
$global->save($table,$data);
//$global->save($table,$data);
$this->local_global['public'][$table]=$data;

//最后数据更新时间
$con->tag_data_time=time();
Expand Down Expand Up @@ -223,6 +226,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
9 changes: 7 additions & 2 deletions src/Api/WebSocketV5/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ protected function init(){

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

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

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

Expand Down Expand Up @@ -117,6 +119,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 @@ -143,7 +146,8 @@ protected function getData($global,$callback=null,$sub=[]){
[instId] => BTC-USDT
)*/

$data=$global->get($k);
//$data=$global->get($k);
$data=$global_local['public'][$k];
$temp[$k]=$data;
}
}
Expand All @@ -152,7 +156,8 @@ protected function getData($global,$callback=null,$sub=[]){
foreach ($sub as $k=>$v){
if(!array_key_exists('key',$v)){
$table=json_encode($v);
$data=$global->get($table);
//$data=$global->get($table);
$data=$global_local['public'][$table];
} else {
$keysecret=[
'key'=>$v['key'],
Expand Down
9 changes: 8 additions & 1 deletion src/Api/WebSocketV5/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 @@ -132,7 +133,8 @@ private function onMessage($global){
$table=$this->userKey($con->tag_keysecret,$table);
$global->saveQueue($table,$data);
}else{
$global->save($table,$data);
//$global->save($table,$data);
$this->local_global['public'][$table]=$data;

//最后数据更新时间
$con->tag_data_time=time();
Expand Down Expand Up @@ -244,6 +246,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 f991d43

Please sign in to comment.