-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathws-spot-public.ts
99 lines (80 loc) · 2.43 KB
/
ws-spot-public.ts
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
97
98
99
import {
WebsocketClient,
// WsSpotOperation,
} from '../src';
// import from npm, after installing via npm `npm install bitmart-api`
// import { WebsocketClient } from 'bitmart-api';
async function start() {
const client = new WebsocketClient();
client.on('open', (data) => {
console.log('open: ', data?.wsKey);
// Some topics allow requests, here's an example for sending a request
// const wsKey = 'spotPublicV1';
// if (data?.wsKey === wsKey) {
// const depthIncreaseDataRequest: WsSpotOperation = {
// op: 'request',
// args: ['spot/depth/increase100:BTC_USDT'],
// };
// client.tryWsSend(
// 'spotPublicV1',
// JSON.stringify(depthIncreaseDataRequest),
// );
// }
});
// Data received
client.on('update', (data) => {
console.info('data received: ', JSON.stringify(data));
});
// Something happened, attempting to reconenct
client.on('reconnect', (data) => {
console.log('reconnect: ', data);
});
// Reconnect successful
client.on('reconnected', (data) => {
console.log('reconnected: ', data);
});
// Connection closed. If unexpected, expect reconnect -> reconnected.
client.on('close', (data) => {
console.error('close: ', data);
});
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
client.on('response', (data) => {
console.info('response: ', data);
});
client.on('exception', (data) => {
console.error('exception: ', data);
});
try {
/**
* Use the client subscribe(topic, market) pattern to subscribe to any websocket topic.
*
* You can subscribe to topics one at a time:
*/
// Ticker Channel
// client.subscribe('spot/ticker:BTC_USDT', 'spot');
// KLine/Candles Channel
// client.subscribe('spot/kline1m:BTC_USDT', 'spot');
// Depth-All Channel
// client.subscribe('spot/depth5:BTC_USDT', 'spot');
// Depth-Increase Channel
// client.subscribe('spot/depth/increase100:BTC_USDT', 'spot');
// Trade Channel
// client.subscribe('spot/trade:BTC_USDT', 'spot');
/**
* Or have multiple topics in one array, in a single request:
*/
client.subscribe(
[
'spot/ticker:BTC_USDT',
'spot/ticker:ETH_USDT',
'spot/ticker:XRP_USDT',
'spot/ticker:BMX_USDT',
'spot/ticker:SOL_USDT',
],
'spot',
);
} catch (e) {
console.error(`Req error: `, e);
}
}
start();