-
Notifications
You must be signed in to change notification settings - Fork 14
/
cryptocurrency-markets-websocket.min.js
2 lines (2 loc) · 2.93 KB
/
cryptocurrency-markets-websocket.min.js
1
2
/* Real-time Cryptocurrency Market Prices Websocket | v1.0 | executium.com/license */
var sockets=new Array,cntrlProp=new Array,monitor=[],wssBASE="wss-public.executium.com";function socket_connect(o,e,n){try{console.log("ID: "+o+" | Attempting: "+e),sockets[o]=io(e,{reconnection:!0,reconnectionDelay:500,reconnectionDelayMax:500,reconnectionAttempts:100}),sockets[o].on("connect",function(){console.log("ID: "+o+" | Connected: "+e),socket_output(o,n,"connect")}),sockets[o].on(o,"disconnect",function(){console.log("ID: "+o+" Disconnected: "+e),socket_output(o,n,"disconnect")}),sockets[o].on("connect_error",function(t){console.log("ID: "+o+" | Error: "+e,t),socket_output(o,n,"connectionerror")})}catch(o){console.log("Socket - Internal Data Issue",o)}}function socket_output(o,e,n){switch(e){case"obreq":manage_orderbook_request(o,n);break;case"ob":manage_orderbook_data(o,n)}}function request_orderbook_server(o,e,n){var t=n+"-"+o+"-"+e;sockets[wssBASE].send({req:o,s:e,o:t})}function manage_orderbook_data(o,e){sockets[o].on("mvsym",function(e){console.log("Symbol Moved Server ->"+e.n,e),unsubscribe_from_orderbook(o,cntrlProp[e.n]);var n=e.n.split("-");$("."+e.n+"-price").empty().html(""),$("."+e.n+"-qty").empty().html(""),$("."+e.n+"-ago").empty().html(""),$("."+e.n+"-com").empty().html(""),request_orderbook_server(n[1],n[2],e.n)}),sockets[o].on("dp",function(e){try{var n=JSON.parse(e.d),t=e.n.replace("/","-"),r=(new Date).getTime()-n[2],s=t.split("-")[0],c=parseFloat(n[0]).toFixed(8);if(c<1)var a=c;else a=(a=numeral(c)).format("0,0.00000000");var i=numeral(n[1]);if($(".price-"+t).empty().html(a),$(".qty-"+t).empty().html(i.format("0,0.00000000")),$(".ago-"+t).empty().html(r),$(".server-"+t).empty().html(o),"bids"==s){void 0===monitor[t]&&(monitor[t]=a);var l="",m=!0;if(a<monitor[t])l="transition-down";if(a>monitor[t])l="transition-up";if(r>3e5){l="transition-bad";m=!1}""!=l&&($(".row-"+t).addClass(l),!0===m&&setTimeout(function(){$(".row-"+t).removeClass(l)},110)),monitor[t]=a}}catch(o){console.log("error",o)}})}function manage_orderbook_request(o,e){sockets[o].on("obreq",function(o){var e=0;void 0===sockets[o.n]&&("notavailable"==o.n?(console.warn("Issue",o),$("."+o.o+"-price").empty().html("Not available or running - "+o.s),e=-1):(socket_connect(o.n,"https://"+o.n+":2083","ob"),e=1e3)),e>-1&&setTimeout(function(){var e=o.o.split("-");"bids"!=e[0]&&"asks"!=e[0]||controllerOb(o.n,o.o,e[0],o.s,1,o.o)},e)})}function controllerOb(o,e,n,t,r,s){var c=n+"/"+t+"-"+r;void 0!==cntrlProp[e]&&unsubscribe_from_orderbook(o,cntrlProp[e]),$("."+s+"-price").empty().html('<span class="price-'+c.replace("/","-")+'"></span>'),$("."+s+"-qty").empty().html('<span class="qty-'+c.replace("/","-")+'"></span>'),$("."+s+"-ago").empty().html('<span class="ago-'+c.replace("/","-")+' ago"></span>'),cntrlProp[e]=c,subscribe_to_orderbook(o,c)}function unsubscribe_from_orderbook(o,e){sockets[o].send({unsubscribe:e})}function subscribe_to_orderbook(o,e){sockets[o].send({subscribe:e})}