|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Codenixsv\BitfinexWs; |
|
6
|
|
|
|
|
7
|
|
|
use Codenixsv\BitfinexWs\Request\Request; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Ratchet\RFC6455\Messaging\Message; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class BitfinexClient |
|
13
|
|
|
* @package Codenixsv\BitfinexWs |
|
14
|
|
|
*/ |
|
15
|
|
|
class BitfinexClient |
|
16
|
|
|
{ |
|
17
|
|
|
private const PUBLIC_CHANNELS_URL = 'wss://api-pub.bitfinex.com/ws/2'; |
|
18
|
|
|
private const AUTHENTICATED_CHANNELS_URL = 'wss://api.bitfinex.com/ws/2'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var BaseWsClient */ |
|
21
|
|
|
private $client; |
|
22
|
|
|
|
|
23
|
|
|
/** @var callable */ |
|
24
|
|
|
private $onReject; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* BitfinexClient constructor. |
|
28
|
|
|
* @param BaseWsClient|null $client |
|
29
|
|
|
*/ |
|
30
|
5 |
|
public function __construct(?BaseWsClient $client = null) |
|
31
|
|
|
{ |
|
32
|
5 |
|
$this->client = $client ?: new WsClient(); |
|
33
|
|
|
|
|
34
|
|
|
$this->onReject = static function (Exception $exception) { |
|
35
|
1 |
|
echo $exception->getMessage(); |
|
36
|
1 |
|
}; |
|
37
|
5 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Request[] $requests |
|
41
|
|
|
* @param array $events |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function connect(array $requests, array $events) |
|
44
|
|
|
{ |
|
45
|
2 |
|
$this->client->connect( |
|
46
|
2 |
|
$this->getPublicChannelsUrl(), |
|
47
|
2 |
|
$this->createMessage($requests), |
|
48
|
|
|
$events, |
|
49
|
2 |
|
$this->onReject |
|
50
|
|
|
); |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $requests |
|
55
|
|
|
* @param array $events |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function authConnect(array $requests, array $events) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->client->connect( |
|
60
|
1 |
|
$this->getAuthenticatedChannelsUrl(), |
|
61
|
1 |
|
$this->createMessage($requests), |
|
62
|
|
|
$events, |
|
63
|
1 |
|
$this->onReject |
|
64
|
|
|
); |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function getPublicChannelsUrl(): string |
|
71
|
|
|
{ |
|
72
|
2 |
|
return self::PUBLIC_CHANNELS_URL; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getAuthenticatedChannelsUrl(): string |
|
79
|
|
|
{ |
|
80
|
1 |
|
return self::AUTHENTICATED_CHANNELS_URL; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
public function getOnReject(): callable |
|
84
|
|
|
{ |
|
85
|
2 |
|
return $this->onReject; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Request[] $requests |
|
90
|
|
|
* @return Message |
|
91
|
|
|
*/ |
|
92
|
3 |
|
private function createMessage(array $requests): Message |
|
93
|
|
|
{ |
|
94
|
3 |
|
$message = new Message(); |
|
95
|
|
|
|
|
96
|
3 |
|
foreach ($requests as $request) { |
|
97
|
1 |
|
$message->addFrame($request->getFrame()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
return $message; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|