Skip to content

Commit

Permalink
add queue
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Dec 2, 2020
1 parent db06efb commit f81aefe
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
25 changes: 18 additions & 7 deletions src/Api/WebSocket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,42 @@ protected function getData($global,$callback=null,$sub=[]){
//默认返回所有数据
if(empty($sub)){
foreach ($all_sub as $k=>$v){
if(is_array($v)) $table=$k;
else $table=$v;
if(is_array($v)){
if(empty($this->keysecret) || $this->keysecret['key']!=$k) continue;

$data=$global->get(strtolower($table));
if(empty($data)) continue;
$temp[$table]=$data;
foreach ($v as $vv){
$data=$global->getQueue(strtolower($vv));
$temp[strtolower($vv)]=$data;
}
}else{
$data=$global->get(strtolower($v));
$temp[strtolower($v)]=$data;
}
}
}else{
//die('hh');
//返回规定的数据
if(!empty($this->keysecret)) {
//是否有私有数据
if(isset($all_sub[$this->keysecret['key']])) $sub=array_merge($sub,$all_sub[$this->keysecret['key']]);
}

//现货 maket数据key 无法对应。默认返回全部maket??
if($this->getPlatform()=='spot'){

}

//print_r($sub);
foreach ($sub as $k=>$v){
//判断私有数据是否需要走队列数据
$temp_v=explode('=',$v);
$temp_v=explode(self::$USER_DELIMITER,$v);
if(count($temp_v)>1){
//private
$data=$global->getQueue(strtolower($v));
}else{
//public
$data=$global->get(strtolower($v));
}

if(empty($data)) continue;

$temp[$v]=$data;
Expand Down
5 changes: 4 additions & 1 deletion src/Api/WebSocket/SocketFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

trait SocketFunction
{
//标记分隔符
static $USER_DELIMITER='===';

/**
* @param array $sub
* @return array
Expand Down Expand Up @@ -65,7 +68,7 @@ protected function log($message){
* @param $keysecret
*/
protected function userKey(array $keysecret,string $sub){
return $keysecret['key'].'='.$sub;
return $keysecret['key'].self::$USER_DELIMITER.$sub;
}

/**
Expand Down
21 changes: 17 additions & 4 deletions src/Api/WebSocket/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,18 @@ private function onMessage($global){
return;
}

//pulibc market
//market kline
if(isset($data['ch']) && $data['ch']!='auth') {
$table=strtolower($data['ch']);
$global->save($table,$data);

//私有数据存入队列
if(!in_array($con->tag,$this->public_url)) {
$table=$this->userKey($con->tag_keysecret,$table);
$global->saveQueue($table,$data);
}else{
$global->save($table,$data);
}

return;
}

Expand Down Expand Up @@ -215,8 +223,13 @@ private function onMessage($global){

if(isset($data['topic'])) {
$table=strtolower($data['topic']);
if(!in_array($con->tag,$this->public_url)) $table=$this->userKey($con->tag_keysecret,$table);
$global->saveQueue($table,$data);
if(!in_array($con->tag,$this->public_url)) {
$table=$this->userKey($con->tag_keysecret,$table);
$global->saveQueue($table,$data);
}else{
$global->save($table,$data);
}

return;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/websocket/client_future.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
//****Three ways return to the specified channel data,All private data is also returned by default

//The first way
$huobi->keysecret($key_secret[0]);
/*$huobi->keysecret($key_secret[0]);
$data=$huobi->getSubscribe([
'market.BTC_CQ.depth.step0',
//'market.ETH_CQ.depth.step0',
Expand All @@ -226,7 +226,7 @@
//'trigger_order.eos',
]);
print_r(json_encode($data));

//die;
//The second way callback
$huobi->keysecret($key_secret[0]);
$huobi->getSubscribe([
Expand All @@ -239,12 +239,12 @@
//'trigger_order.eos',
],function($data){
print_r(json_encode($data));
});
});*/

//The third way is to guard the process
$huobi->keysecret($key_secret[0]);
$huobi->getSubscribe([
'market.BTC_CQ.depth.step0',
//'market.BTC_CQ.depth.step0',
//'market.ETH_CQ.depth.step0',

'orders.eos',
Expand Down
1 change: 1 addition & 0 deletions tests/websocket/client_spot.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
//****Three ways to get all data

//The first way
$huobi->keysecret($key_secret[0]);
$data=$huobi->getSubscribes();
print_r(json_encode($data));
die;
Expand Down

0 comments on commit f81aefe

Please sign in to comment.