JSFiddle - React, Tailwind, and code Playground

by humanzing

JavaScript

console.clear();

let exchange = {
  binance: {
    host: "wss://fstream.binance.com/stream?streams=",
    ws: null,
    ws_path: "",
    started: false,
    liquidation: {
      type: "path",
      path: "{token}@forceOrder/",
    },
    orderbook: "",
    trade: {
      type: "path",
      path: "{token}@aggTrade/"
    },
  },
  bybit: {
    host: "wss://stream.bybit.com/realtime",
    ws: null,
    ws_path: "",
    started: false,
    liquidation: {
      type: "msg",
      msg: '{"op":"subscribe","args":["liquidation.{token}"]}',
    },
    orderbook: "",
    trade: {
      type: "msg",
      msg: '{"op": "subscribe", "args": ["trade.{token}"]}',
    },
  },
  bitmex: {
    host: "",
    liquidation: "",
    orderbook: "",
    trade: "",
  },
}




function new_message(exchange, data) {
  console.log("new message from " + exchange + " " + data)
}

function link_event(e, ex) {
  e.ws.onopen = function() {
    console.log(ex + " open")
    ws_binance_connected = true;
  };

  e.ws.onclose = function() {
    console.log(ex + "close")
    ws_binance_connected = false;
  };

  e.ws.onmessage = function(message) {
    new_message(ex, message.data);
  };

}

function start_ws(ex, type, token = "") {

  try {
    if (exchange[ex][type].type == "path") {
      if (exchange[ex].started) {
        exchange[ex].ws.close();
        exchange[ex].ws = null;
      }
      if (exchange[ex].ws_path == "") {
        exchange[ex].ws_path = exchange[ex].host + exchange[ex][type].path.replace("{token}", token)
      } else {
        exchange[ex].ws_path += exchange[ex][type].path.replace("{token}", token)
      }
      alert(exchange[ex].ws_path)
      exchange[ex].ws = new WebSocket(exchange[ex].ws_path);
      exchange[ex].started = true
      link_event(exchange[ex], ex)
    } else if (exchange[ex][type].type == "msg") {
      if (!exchange[ex].started) {
        exchange[ex].ws = new WebSocket(exchange[ex].host);
        link_event(exchange[ex], ex)
      }
      let message =...