-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.ts
More file actions
62 lines (49 loc) · 1.62 KB
/
websocket.ts
File metadata and controls
62 lines (49 loc) · 1.62 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
/**
* WebSocket gerçek zamanlı örnek
*
* Çalıştırma:
* ALTINAPI_KEY=hapi_xxx npx tsx examples/websocket.ts
*/
import { AltinapiClient } from '../src/index.js';
const apiKey = process.env.ALTINAPI_KEY;
if (!apiKey) {
console.error('ALTINAPI_KEY environment değişkeni eksik');
process.exit(1);
}
const client = new AltinapiClient({ apiKey });
client.connect();
client.on('connect', () => {
console.log('✅ altinapi WebSocket\'e bağlandı');
client.subscribe(['USDTRY', 'EURTRY', 'ALTIN', 'CEYREK_YENI']);
console.log('📡 Abone olundu: USDTRY, EURTRY, ALTIN, CEYREK_YENI');
});
client.on('prices:snapshot', (fiyatlar) => {
console.log(`\n📸 Snapshot alındı (${fiyatlar.length} sembol)`);
for (const f of fiyatlar.slice(0, 5)) {
console.log(` ${f.symbol.padEnd(15)} ${f.bid} / ${f.ask}`);
}
});
client.on('prices:update', (guncellemeler) => {
for (const f of guncellemeler) {
const saat = new Date(f.timestamp).toLocaleTimeString('tr-TR');
console.log(`[${saat}] ${f.symbol.padEnd(15)} alış=${f.bid} satış=${f.ask}`);
}
});
client.on('data:stale', () => {
console.warn('⚠️ Kaynak bağlantısı koptu — fiyatlar eski olabilir');
});
client.on('data:live', () => {
console.log('✅ Kaynak yeniden bağlandı');
});
client.on('disconnect', () => {
console.log('❌ Bağlantı koptu');
});
client.on('connect_error', (err) => {
console.error('Bağlantı hatası:', err.message);
});
process.on('SIGINT', () => {
console.log('\n👋 Kapanıyor...');
client.disconnect();
process.exit(0);
});
console.log('🔌 altinapi\'ye bağlanılıyor... (çıkmak için Ctrl+C)');