-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathExampleWs.php
More file actions
96 lines (82 loc) · 3.28 KB
/
Copy pathExampleWs.php
File metadata and controls
96 lines (82 loc) · 3.28 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
use KuCoin\UniversalSDK\Api\DefaultClient;
use KuCoin\UniversalSDK\Common\Logger;
use KuCoin\UniversalSDK\Generate\Spot\SpotPublic\AllTickersEvent;
use KuCoin\UniversalSDK\Model\ClientOptionBuilder;
use KuCoin\UniversalSDK\Model\Constants;
use KuCoin\UniversalSDK\Model\TransportOptionBuilder;
use KuCoin\UniversalSDK\Model\WebSocketClientOptionBuilder;
use React\EventLoop\Loop;
include '../vendor/autoload.php';
function wsExample()
{
// Credentials & setup
$key = getenv('API_KEY') ?: '';
$secret = getenv('API_SECRET') ?: '';
$passphrase = getenv('API_PASSPHRASE') ?: '';
$brokerName = getenv('BROKER_NAME');
$brokerKey = getenv('BROKER_KEY');
$brokerPartner = getenv('BROKER_PARTNER');
$httpTransportOption = (new TransportOptionBuilder())
->setKeepAlive(true)
->setMaxConnections(10)
->build();
$websocketTransportOption = (new WebSocketClientOptionBuilder())->build();
$clientOption = (new ClientOptionBuilder())
->setKey($key)
->setSecret($secret)
->setPassphrase($passphrase)
->setBrokerName($brokerName)
->setBrokerKey($brokerKey)
->setBrokerPartner($brokerPartner)
->setSpotEndpoint(Constants::GLOBAL_API_ENDPOINT)
->setFuturesEndpoint(Constants::GLOBAL_FUTURES_API_ENDPOINT)
->setBrokerEndpoint(Constants::GLOBAL_BROKER_API_ENDPOINT)
->setTransportOption($httpTransportOption)
->setWebSocketClientOption($websocketTransportOption)
->build();
// Create or get the global event loop
$loop = Loop::get();
$client = new DefaultClient($clientOption, $loop);
$spotWs = $client->wsService()->newSpotPublicWS();
// Start connection
$spotWs->start()->then(function () use ($spotWs, $loop) {
Logger::info("WebSocket started");
// Subscribe to allTickers
return $spotWs->allTickers(
// Called when data is received
function (string $topic, string $subject, AllTickersEvent $data) {
Logger::info("Ticker update", [
'topic' => $topic,
'subject' => $subject,
'bestBid' => $data->bestBid,
'bestAsk' => $data->bestAsk,
]);
},
// Called when subscription is successful
function (string $id) use ($spotWs, $loop) {
Logger::info("Subscribed with ID: $id");
// Schedule unsubscribe and shutdown after 5 seconds
$loop->addTimer(5, function () use ($id, $spotWs) {
Logger::info("Unsubscribing...");
$spotWs->unSubscribe($id)->finally(function () use ($spotWs) {
$spotWs->stop();
});
});
},
// Called when subscription fails
function (Exception $e) use ($spotWs) {
Logger::error("Subscription failed", ['error' => $e->getMessage()]);
$spotWs->stop();
}
);
})->catch(function (Exception $e) use ($spotWs) {
Logger::error("Failed to start", ['error' => $e->getMessage()]);
$spotWs->stop();
});
// Run the event loop to process async tasks
$loop->run();
}
if (php_sapi_name() === 'cli') {
wsExample();
}