-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebSocketListener.js
More file actions
45 lines (41 loc) · 1.41 KB
/
webSocketListener.js
File metadata and controls
45 lines (41 loc) · 1.41 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
require('dotenv').config();
const WebSocket = require('ws');
const { Candlestick } = require('./models/candlestick.model');
const db = require('./controllers/candlestick.controller');
const init = (request, response) => {
const ws = new WebSocket(`${process.env.BINANCE_FUTURES_WSS}${request.Symbol}@kline_${request.Interval}`);
ws.on("open", function open() {
console.log(`WebSocket ${request.Symbol} successfully connected!`);
});
ws.on("close", function open() {
console.log(`WebSocket ${request.Symbol} Closed`);
});
ws.on("error", function open(err) {
console.log(`WebSocket ${request.Symbol} Error`);
});
ws.on("message", (data) => {
if (data) {
const trade = JSON.parse(data);
if (trade.k.x) {
const candlestick = new Candlestick();
const openTime = new Date(trade.k.t);
const closeTime = new Date(trade.k.T);
candlestick.Name = trade.s;
candlestick.OpenTime = openTime;
candlestick.OpenPrice = trade.k.o;
candlestick.HighPrice = trade.k.h;
candlestick.LowPrice = trade.k.l;
candlestick.ClosePrice = trade.k.c;
candlestick.Volume = trade.k.V;
candlestick.CloseTime = closeTime;
candlestick.Interval = 3;
candlestick.Symbol = trade.s;
candlestick.IsActive = true;
console.log(db.createCandlestick(candlestick));
}
}
});
};
module.exports = {
init,
};