-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump-json-private-ws.mjs
52 lines (44 loc) · 1.28 KB
/
dump-json-private-ws.mjs
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
import * as qs from 'querystring';
import { URL } from 'url';
import { createHmac } from 'crypto';
import { request as httpsRequest } from 'https';
import WebSocket from 'ws';
const apiKey = '';
const apiSec = '';
const request = (url, options) => new Promise((res, rej) => {
let reply = '';
const req = httpsRequest(url, options, (rr) => {
rr.on('data', (d) => {
reply += d.toString();
});
rr.on('error', (e) => rej(e));
rr.on('end', () => res(reply));
});
req.end();
});
function getSignedURL(baseURL, sigObj) {
const u = new URL(baseURL);
const hasher = createHmac('sha256', apiSec);
hasher.update(qs.stringify(sigObj));
u.search = qs.stringify({
...sigObj,
signature: hasher.digest('hex'),
});
return u;
}
(async () => {
async function getListenKey() {
const reqUrl = getSignedURL('https://fapi.binance.com/fapi/v1/listenKey', {});
const lk = await request(reqUrl, { method: 'POST', headers: { 'X-MBX-APIKEY': apiKey } });
return JSON.parse(lk).listenKey;
}
const lk = await getListenKey();
const ws = new WebSocket(`wss://fstream.binance.com/ws/${lk}`);
ws.on('open', () => {
console.log(`ws open; lk=${lk}`);
});
ws.on('message', (msg) => {
console.log(msg.toString());
});
// ws.on('close',() => {})
})();