I can give you an article showing you how to achieve this using JavaScript, HTML and CSS.
Ethereum: Protect the console magazine score massively every 3 seconds
In this example, we will use the knot. We will also use Websockets to update the customer every 3 seconds.
server side (Node.js)
`Javascript
Const Express = ASK (‘Express’);
CONST App = Express ();
Const Websocket = ASK (‘ws’);
CONST WSS = New Websocket.server ({port: 8080});
App.get (‘/api/btc/price’, (req, res) => {
Const nest = wss.cept (req, res);
CONST Prices = [];
Socket.on (‘message’, (data) => {
ifon.parsse (data) .Symbol == ‘btc’) {
prices.pusk (json.parsse (data) .Latestprice);
Console.log (Last price: $ {prices [prices.lengh - 1]}
);
// Specify the last price of the customer
Socket.send (json.stringify ({symbol: ‘btc’, the latest process: prices [prices.garums – 1]}));
}
});
Socket.on (‘close’, () => {
Wss.close ();
});
});
App .listen (8080, () => {
Console.log (‘server listening port 8080’);
});
`
Customer Party (HTML and JavaScript)
`html
#Caine container {
Font size: 24 pixels;
Font weight: highlighted;
Margin-bottom: 20 pixels;
}
Shead>
Binance price tracking tool
CONST NEST = New Web Boot ('WSS: //api.binance.com: 8080/API/BTC/PRICE');
Document.adventlistener ('Domcontotoded', () => {
Allow prices = [];
Nest.onmessage = (event) => {
If (json.parsse (Event.data) .Symbol == 'btc') {
prices.pusk (json.Parsse (Event.data) .Latestprice);
Console.log (Last price: $ {prices [prices.lengh - 1]});
}
};
Document.getmentbyid (Price -Kontainer). Innertext =The latest Bitcoin price: $ {prices [prices.lengt - 1]};
});
Nest.onclose = () => {
Document.getmentbyid ('Price containing'). Innertext = "No new prices";
};
script>
Poom>
html>
Explanation
- We create an Express.js server that listens to the port 8080 and accepts Websocket.
The final point /API/BTC/Price
is a get request that returns the latest Bitcoin price from Binance. When it comes, he analyzes JSON data and saves the price at huge "prices".
- In each report we receive, we check that the symbol is BTC. If so, we analyze the data and increase the new price to the end of the "price" array.
- We also record the last price console for coordination purposes.
- When the Websocket connection is closed, we update the user interface with a message stating that there are no new prices.
To update the page every time you want the last price, just upload the website by clicking the "Reboot prices" button below the price container. The server will automatically update and increase new prices for the customer.
Để lại một bình luận