Advertisement
Guest User

Function for select node

a guest
Sep 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function checkWorkingNode() {
  2.     const NODES = [
  3.         "wss://golos.lexa.host/ws",
  4.         "wss://api.golos.blckchnd.com/ws"
  5.     ];
  6.     let node = localStorage.getItem("node") || NODES[0];
  7.     const idx = Math.max(NODES.indexOf(node), 0);
  8.     let checked = 0;
  9.     const find = (idx) => {
  10.         if (idx >= NODES.length) {
  11.             idx = 0;
  12.         }
  13.         if (checked >= NODES.length) {
  14.             alert("no working nodes found");
  15.             return;
  16.         }
  17.         node = NODES[idx];
  18.         console.log("check", idx, node);
  19.         golos.config.set("websocket", node);
  20.         try {
  21.             golos.api.stop();
  22.         } catch(e) {
  23.         }
  24.        
  25.         let timeout = false;
  26.         let timer = setTimeout(() => {
  27.             console.log("timeout", NODES[idx])
  28.             timeout = true;
  29.             find(idx + 1);
  30.         }, 3000);
  31.         golos.api.getDynamicGlobalPropertiesAsync()
  32.             .then(props => {
  33.                 if(!timeout) {
  34.                     check = props.head_block_number;
  35.                     console.log("found working node", node);
  36.                     localStorage.setItem("node", node);
  37.                     clearTimeout(timer);
  38.                 }
  39.             })
  40.             .catch(e => {
  41.                 console.log("connection error", node, e);
  42.                 find(idx + 1);
  43.             });
  44.     }
  45.     find(idx);
  46. }
  47. checkWorkingNode();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement