Skip to content

Commit

Permalink
Get partial private channel subscription data
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Oct 23, 2020
1 parent ec6a6c6 commit a84ac93
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 26 deletions.
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ $okex->unsubscribe([
]);
```

There are three ways to obtain channel data
Get all channel subscription data
```php

//The first way
Expand All @@ -639,4 +639,59 @@ $okex->getSubscribe(function($data){
print_r(json_encode($data));
},true);
```

Get partial channel subscription data
```php
//The first way
$data=$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
]);
print_r(json_encode($data));

//The second way callback
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
});

//The third way is to guard the process
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
},true);
```

Get partial private channel subscription data
```php
//The first way
$okex->keysecret($key_secret);
$data=$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
]);
print_r(json_encode($data));

//The second way callback
$okex->keysecret($key_secret);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
});

//The third way is to guard the process
$okex->keysecret($key_secret);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
},true);
```
[More Test](https://github.com/zhouaini528/okex-php/tree/master/tests/websocket/client.php)
65 changes: 60 additions & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,24 +619,79 @@ $okex->unsubscribe([
]);
```

频道数据获取,有三种方式
获取全部频道订阅数据
```php

//第一种方式,直接获取当前最新数据
$data=$okex->getSubscribe();
$data=$okex->getSubscribes();
print_r(json_encode($data));


//第二种方式,通过回调函数,获取当前最新数据
$okex->getSubscribe(function($data){
$okex->getSubscribes(function($data){
print_r(json_encode($data));
});

//第二种方式,通过回调函数并开启常驻进程,获取当前最新数据
$okex->getSubscribe(function($data){
$okex->getSubscribes(function($data){
print_r(json_encode($data));
},true);
```

获取部分频道订阅数据
```php
//The first way
$data=$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
]);
print_r(json_encode($data));

//The second way callback
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
});

//The third way is to guard the process
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
},true);
```

获取部分私有频道订阅数据
```php
//The first way
$okex->keysecret($key_secret);
$data=$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
]);
print_r(json_encode($data));

//The second way callback
$okex->keysecret($key_secret);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
});

//The third way is to guard the process
$okex->keysecret($key_secret);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
},true);
```

[更多用例请查看](https://github.com/zhouaini528/okex-php/tree/master/tests/websocket/client.php)


65 changes: 52 additions & 13 deletions src/Api/WebSocket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,78 @@ public function unsubscribe(array $sub=[]){
* @param bool $daemon
* @return mixed
*/
public function getSubscribe($callback=null,$daemon=false){
public function getSubscribe(array $sub,$callback=null,$daemon=false){
if($daemon) $this->daemon($callback,$sub);

$sub=$this->resub($sub);

return $this->getData($this,$callback,$sub);
}

/**
* 返回订阅的所有数据
* @param null $callback
* @param bool $daemon
* @return array
*/
public function getSubscribes($callback=null,$daemon=false){
if($daemon) $this->daemon($callback);

return $this->getData($this,$callback);
}

protected function daemon($callback=null){
protected function daemon($callback=null,$sub=[]){
$worker = new Worker();
$worker->onWorkerStart = function() use($callback) {
$worker->onWorkerStart = function() use($callback,$sub) {
$global = $this->client();

$time=isset($this->config['data_time']) ? $this->config['data_time'] : 0.1 ;

Timer::add($time, function() use ($global,$callback){
$this->getData($global,$callback);
$sub=$this->resub($sub);

Timer::add($time, function() use ($global,$callback,$sub){
$this->getData($global,$callback,$sub);
});
};
Worker::runAll();
}

protected function getData($global,$callback=null){
/**
* @param $global
* @param null $callback
* @param array $sub 返回规定的频道
* @return array
*/
protected function getData($global,$callback=null,$sub=[]){
$all_sub=$global->get('all_sub');
if(empty($all_sub)) return [];

$temp=[];
foreach ($all_sub as $k=>$v){
if(is_array($v)) $table=$k;
else $table=$v;

$data=$global->get(strtolower($table));
if(empty($data)) continue;

$temp[$table]=$data;
//默认返回所有数据
if(empty($sub)){
foreach ($all_sub as $k=>$v){
if(is_array($v)) $table=$k;
else $table=$v;

$data=$global->get(strtolower($table));
if(empty($data)) continue;
$temp[$table]=$data;
}
}else{
//返回规定的数据
foreach ($sub as $k=>$v){
if(count($v)==1) $table=$v[0];
else {
//private
if(!isset($v[1]['key'])) continue;
$table=$this->userKey($v[1],$v[0]);
}
$data=$global->get(strtolower($table));
if(empty($data)) continue;

$temp[$table]=$data;
}
}

if($callback!==null){
Expand Down
10 changes: 9 additions & 1 deletion src/Api/WebSocket/SocketFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function resub(array $sub=[]){
$temp2=[$v];
foreach ($temp1 as $tv){
if(strpos($v, $tv) !== false){
array_push($temp2,$this->keysecret);
array_push($temp2,empty($this->keysecret)? [] : $this->keysecret);
}
}
array_push($new_sub,$temp2);
Expand Down Expand Up @@ -84,4 +84,12 @@ protected function log($message){

echo $message;
}

/**
* 设置用户key
* @param $keysecret
*/
protected function userKey(array $keysecret,string $sub){
return $keysecret['key'].'='.$sub;
}
}
2 changes: 1 addition & 1 deletion src/Api/WebSocket/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function onMessage($global){
$table=$data['table'].':'.$this->getInstrumentId($data);
$table=strtolower($table);

if($con->tag != 'public') $table=$con->tag_keysecret['key'].$table;
if($con->tag != 'public') $table=$this->userKey($con->tag_keysecret,$table);

$global->save($table,$data);
return;
Expand Down
8 changes: 6 additions & 2 deletions src/OkexWebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function unsubscribe(array $sub=[]){
$this->client()->unsubscribe($sub);
}

public function getSubscribe($callback=null,$daemon=false){
return $this->client()->getSubscribe($callback,$daemon);
public function getSubscribe(array $sub,$callback=null,$daemon=false){
return $this->client()->getSubscribe($sub,$callback,$daemon);
}

public function getSubscribes($callback=null,$daemon=false){
return $this->client()->getSubscribes($callback,$daemon);
}
}
82 changes: 79 additions & 3 deletions tests/websocket/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,78 @@
//****Three ways to get all data

//The first way
$data=$okex->getSubscribe();
$data=$okex->getSubscribes();
print_r(json_encode($data));


//The second way callback
$okex->getSubscribe(function($data){
$okex->getSubscribes(function($data){
print_r(json_encode($data));
});

//The third way is to guard the process
$okex->getSubscribe(function($data){
$okex->getSubscribes(function($data){
print_r(json_encode($data));
},true);

break;
}

case 21:{
//****Three ways return to the specified channel data

//The first way
$data=$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
]);
print_r(json_encode($data));

//The second way callback
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
});

//The third way is to guard the process
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
},true);

break;
}

case 22:{
//****Three ways return to the specified channel data

//The first way
$okex->keysecret($key_secret[0]);
$data=$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
]);
print_r(json_encode($data));

//The second way callback
$okex->keysecret($key_secret[0]);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
});

//The third way is to guard the process
$okex->keysecret($key_secret[0]);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
},true);

Expand Down Expand Up @@ -192,6 +253,21 @@
]);
break;
}

//subscribe
case 10006:{
$okex->keysecret($key_secret[1]);
$okex->subscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
'swap/depth5:BCH-USD-SWAP',

'futures/position:BCH-USD-210326',
'futures/account:BCH-USDT',
'swap/position:BCH-USD-SWAP',
]);
break;
}
}


0 comments on commit a84ac93

Please sign in to comment.