ExBroker.php
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace trader\exchange\bybit;
require_once __DIR__ . '/../../struct/ApiInfo.php';
require_once __DIR__ . '/Api.php';
require_once __DIR__ . '/../../../jytools/Websocket.php';
require_once __DIR__ . '/../../../jytools/func.php';
use trader\struct\ApiInfo;
use trader\exchange\bybit\Api as BbApi;
use jytools\Websocket;
use function jytools\output;
class ExBroker
{
private $host = 'wss://stream.bybit.com';
private $pathPri = '/v5/private'; //账户信息path
private $pathPub = '/v5/public/linear'; //行情信息path 永续
private ?ApiInfo $apiInfo;
private BbApi $api;
private ?Websocket $wsAcc;
private ?Websocket $wsKline;
public function __construct(?ApiInfo $apiInfo)
{
$this->apiInfo = $apiInfo;
$this->api = new BbApi($apiInfo);
}
public function setWsHost($host)
{
$this->host = $host;
}
public function setRestHost($host)
{
$this->api->setHost($host);
}
public function accListen(callable $onData) {}
public function klineListen($symbol, $interval, callable $onData)
{
$this->wsKline = new Websocket($this->host . $this->pathPub);
$this->wsKline->connect(
function () use ($symbol, $interval) {
$this->wsKline->push(json_encode([
'op' => 'subscribe',
'args' => [
"kline.{$interval}.{$symbol}"
]
]));
},
function ($data) use ($onData) {
$data = json_decode($data, true);
if (isset($data['data'])) {
$onData($data);
}
},
);
}
}