Last active
June 28, 2021 21:51
-
-
Save jyydev/de4df2b2777bd1c3210dde92bea45649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <title>(BNB/BTC) Binance - Web Socket Stream</title> | |
| <script language="javascript" type="text/javascript"> | |
| var wsUri = "wss://stream.binance.com:9443/ws/bnbbtc@depth5"; | |
| var outputDiv; | |
| var output; | |
| function init() { | |
| outputDiv = document.getElementById("output"); | |
| initWebSocket(); | |
| } | |
| function initWebSocket() { | |
| websocket = new WebSocket(wsUri); | |
| websocket.onopen = function(evt) { onOpen(evt) }; | |
| websocket.onclose = function(evt) { onClose(evt) }; | |
| websocket.onmessage = function(evt) { onMessage(evt) }; | |
| websocket.onerror = function(evt) { onError(evt) }; | |
| } | |
| function onOpen(evt) { | |
| writeToScreen("CONNECTED"); | |
| //doSend("WebSocket rocks"); | |
| } | |
| function onClose(evt) { | |
| writeToScreen("DISCONNECTED"); | |
| websocket.close(); | |
| } | |
| function onMessage(evt) { | |
| output = ""; | |
| var obj = JSON.parse(evt.data); | |
| console.log(obj); | |
| obj["bids"].reverse(); | |
| for (let i=0; i<obj["bids"].length; i++) { | |
| output += "Bid: " + obj["bids"][i][0] + " - Quantity: " + obj["bids"][i][1] + "<br />"; | |
| } | |
| output += "<br />"; | |
| for (let i=0; i<obj["asks"].length; i++) { | |
| output += "Ask: " + obj["asks"][i][0] + " - Quantity: " + obj["asks"][i][1] + "<br />"; | |
| } | |
| writeToScreen(output); | |
| } | |
| function onError(evt) { | |
| writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); | |
| } | |
| function doSend(message) { | |
| writeToScreen("SENT: " + message); | |
| websocket.send(message); | |
| } | |
| function writeToScreen(message) { | |
| outputDiv.innerHTML = message; | |
| //outputDiv.appendChild(message); | |
| } | |
| window.addEventListener("load", init, false); | |
| </script> | |
| <h2>(BNB/BTC) Binance - Web Socket Stream</h2> | |
| <button onclick="initWebSocket()">Start</button> <button onclick="onClose()">Stop</button> | |
| <br /><br /> | |
| <div id="output"></div> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| testing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment