bitcoin - How to keep Websocket API session alive -


I am trying to subscribe to the blockchain.info websocket API for the new block. I basically closed the code and plugged in the blockchain.info parameter. It works, but I think it will be automatically disconnected after some time. How do I keep the session alive?

  & lt ;! DOCTYPE html & gt; & Lt; Meta charset = "UTF-8" /> & Lt; Title & gt; WebSocket Test & lt; / Title & gt; & Lt; Script language = "javascript" type = "text / javascript" & gt; Var wsUri = "wss: //ws.blockchain.info/inv"; Var Output; Function init () {output = document.getElementById ("output"); TestWebSocket (); } Function testWebSocket () {websocket = New WebSocket (wsUri); Websocket.onopen = function (evt) {on open (evt)}; Websocket.onclose = function (evt) {onClose (evt)}; Websocket.onmessage = function (evt) {onMessage (evt)}; Websocket.onerror = function (evt) {onError (evt)}; } Function On Open (EVT) {writeToScreen ("Connect"); DoSend ("{\" session \ ": \" blocks_sub \ "}"); } Function onClose (evt) {writeToScreen ("disconnected"); } Function On Message (EVT) {writeToScreen ('& lt; span style = "color: blue;" & gt; response:' + evt.data + ';); //websocket.close (); } Function on error (EVT) {writeToScreen ('& lt; span style = "color: red;" & gt; error:  gt; + evt.data); } Function doSend (message) {writeToScreen ("SENT:" + message); Websocket.send (message); } Write function screen (message) {var pre = document.createElement ("p"); Pre.style.wordWrap = "break-word"; Pre.innerHTML = Message; Output.appendChild (ex); } Window.addEventListener ("load", init, false); & Lt; / Script & gt; & Lt; H2 & gt; WebSocket Test & lt; / H2 & gt; & Lt; Div id = "output" & gt; & Lt; / Div & gt;  

I just tested, and it looks like:

  & lt; Script src = "// cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> & Lt; Script & gt; $ (Document) .ready (function () {initWebSocket ();}); & Lt; / Script & gt; & Lt; Script language = "javascript" type = "text / javascript" & gt; Function initWebSocket () {// init block channel webococcult (activity, block) var blockchain = new web site ('ws: //ws.blockchain.info: 8335 / inv'); Blockchain.errorerror = function (error) {console.log ('connection.error', error); }; Blockchain.onopen = function () {blockchain.send (JSON.stringify ({"op": "unconfirmed"})); // unsoperirmed activity membership); Blockchain.onmessage = function (message) {var response = JSON.parse (message.data); Var date = new date (0); Date.SsetUTCSeconds (response.x.time); If (response.op == "utx") {var zodiac = 0; (Var i = 0; i & lt; response.x.out.length; i ++) amount + = response.x.out [i]. value; // zodiac is in satoshi // 1btc = 100,000,000 satoshi (https://en.bitcoin.it/wiki/activity) response.amount = zodiac / 100000000; } Console.log (response.op, response); }; }  

Roughly taken from:


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -