-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.php
More file actions
73 lines (58 loc) · 1.89 KB
/
websocket.php
File metadata and controls
73 lines (58 loc) · 1.89 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
<?php
declare(strict_types=1);
/**
* WebSocket gerçek zamanlı örnek.
*
* Çalıştırma:
* ALTINAPI_KEY=hapi_xxx php examples/websocket.php
*/
require __DIR__ . '/../vendor/autoload.php';
use Altinapi\AltinapiClient;
$apiKey = getenv('ALTINAPI_KEY');
if ($apiKey === false || $apiKey === '') {
fwrite(STDERR, "ALTINAPI_KEY environment değişkeni eksik\n");
exit(1);
}
$client = new AltinapiClient(['apiKey' => $apiKey]);
$client->on('connect', function () {
echo "✅ altinapi WebSocket'e bağlandı\n";
});
$client->on('prices:snapshot', function (array $fiyatlar) {
echo "\n📸 Snapshot alındı (" . count($fiyatlar) . " sembol)\n";
foreach (array_slice($fiyatlar, 0, 5) as $f) {
printf(" %-15s %s / %s\n", $f['symbol'] ?? '?', $f['bid'] ?? '?', $f['ask'] ?? '?');
}
});
$client->on('prices:update', function (array $guncellemeler) {
foreach ($guncellemeler as $f) {
$saat = date('H:i:s');
printf("[%s] %-15s alış=%s satış=%s\n",
$saat,
$f['symbol'] ?? '?',
$f['bid'] ?? '?',
$f['ask'] ?? '?'
);
}
});
$client->on('data:stale', function () {
echo "⚠️ Kaynak bağlantısı koptu — fiyatlar eski olabilir\n";
});
$client->on('data:live', function () {
echo "✅ Kaynak yeniden bağlandı\n";
});
$client->on('connect_error', function (\Throwable $err) {
fwrite(STDERR, "Bağlantı hatası: {$err->getMessage()}\n");
});
// Ctrl+C ile çıkış için
pcntl_signal(SIGINT, function () use ($client) {
echo "\n👋 Kapanıyor...\n";
$client->disconnect();
exit(0);
});
echo "🔌 altinapi'ye bağlanılıyor... (Ctrl+C ile çıkış)\n";
$client->connect();
$client->subscribe(['USDTRY', 'EURTRY', 'ALTIN', 'CEYREK_YENI']);
echo "📡 Abone olundu: USDTRY, EURTRY, ALTIN, CEYREK_YENI\n";
// 60 saniye boyunca dinle
$client->listen(60);
$client->disconnect();