-
Notifications
You must be signed in to change notification settings - Fork 5
/
testOrder.mjs
55 lines (48 loc) · 1.7 KB
/
testOrder.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
53
54
55
import fs from "fs";
import WebSocket from "ws";
const app_id = fs.readFileSync("app_id.log", "utf8").trim();
const access_token = fs.readFileSync("access_token.log", "utf8").trim();
const ORDER_URL = "wss://api.fyers.in/socket/v2/orderSock";
const ORDER_QUERY = "user-agent=fyers-api&type=orderUpdate";
const REQUEST = {"T": "SUB_ORD", "SLIST": ["orderUpdate"], "SUB_T": 1};
function mainOrderUsingOn() {
var url = `${ORDER_URL}?${ORDER_QUERY}&access_token=${app_id}:${access_token}`;
var ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.on('error', (err) => {
console.error("Error:", err);
});
ws.on('close', (code, reason) => {
console.log("Closed:", code, reason);
});
ws.on('open', () => {
console.log("Opened:");
ws.send(JSON.stringify(REQUEST));
setInterval(() => ws.send(JSON.stringify('ping')), 5000);
});
ws.on('message', (data, isBinary) => {
if (!isBinary) console.log('Message:', data.toString());
else console.log('Binary message:', data.byteLength);
});
}
function mainOrderUsingProperty() {
var url = `${ORDER_URL}?${ORDER_QUERY}&access_token=${app_id}:${access_token}`;
var ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.onerror = (err) => {
console.error("Error:", err);
};
ws.onclose = (code, reason) => {
console.log("Closed:", code, reason);
};
ws.onopen = () => {
console.log("Opened.");
ws.send(JSON.stringify(REQUEST));
setInterval(() => ws.send(JSON.stringify('ping')), 5000);
};
ws.onmessage = (e) => {
if (typeof e.data==='string') console.log('Message:', e.data);
else console.log('Binary message:', new DataView(e.data).byteLength);
};
}
mainOrderUsingProperty();