-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (53 loc) · 1.48 KB
/
Copy pathindex.js
File metadata and controls
64 lines (53 loc) · 1.48 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
63
64
const express = require('express')
const app = express()
const http = require('http')
const server = http.createServer(app)
const {
Server
} = require('socket.io')
const {
WebSocket
} = require('ws');
const delay = require('delay')
const io = new Server(server)
const axios = require('axios');
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
io.on('connection', (socket) => {
console.log("User Connected")
socket.on('on-chat', data => {
console.log(data)
io.emit('bitcoin-user-chat', data)
})
})
server.listen(3000, () => {
console.log("Listinng on 3000")
})
async function broadcastBitcoinPriceSubscribers() {
let lastPrice = 0
let bitcoinPrice = 0
while (true) {
const response = await axios.get('https://bscexchange.finance/api/v1/fimarketcap/markets/84501')
bitcoinPrice = parseFloat(response.data.current_price) + parseFloat(Math.floor(Math.random() * 10))
if (lastPrice !== bitcoinPrice) {
io.emit('bitcoin-price', { price: parseFloat(bitcoinPrice) })
lastPrice = bitcoinPrice
}
delay(500)
}
}
function connectBinanceSocket() {
const coin = 'btcusdt';
const binanceSocket = new WebSocket(`wss://fstream.binance.com/ws/${coin}@trade`);
binanceSocket.on('message', data => {
if (data) {
const trade = JSON.parse(data);
console.log(trade);
binanceSocket.emit('binance-bitcoin-price', { data: trade })
delay(5000)
}
});
}
broadcastBitcoinPriceSubscribers()
connectBinanceSocket()