小编Pav*_*ngh的帖子

在网络工作者或服务工作者中运行 websocket - javascript

我有来自不同站点的 9 个 websocket 连接,它们正在使用数据更新 DOM。目前我正在连接到所有并监听所有 websockets 并使用函数调用更新数据。

我面临的问题是有很多 websocket 连接,并且存在内存和 CPU 使用问题。如何使用服务工作者和网络工作者来优化如此多的 websocket 连接?

async function appendGatePublicTickersData(e) {
  if (e.event == "update" && e.result[0].contract == "BTC_USD") {
    if ('total_size' in e.result[0]) {
      $(".gate-btc-open-interest").html(commaNumber(e.result[0].total_size))
      if ('last' in e.result[0]) {
        $(".gate-btc-open-value").html(commaNumber(customFixedRounding((e.result[0].total_size / e.result[0].last), 4)))
      }
    }

    if ('volume_24h_usd' in e.result[0]) {
      $(".gate-btc-24-volume").html(commaNumber(e.result[0].volume_24h_usd))
    }

    if ('volume_24h_btc' in e.result[0]) {
      $(".gate-btc-24-turnover").html(commaNumber(e.result[0].volume_24h_btc))
    }

    if ('funding_rate' in e.result[0]) {
      var fundingRateBtcGate = customFixedRounding(e.result[0].funding_rate * 100, 4)
      $(".public-gate-btc-funding").html(fundingRateBtcGate)
    }

    if ('funding_rate_indicative' in e.result[0]) {
      var predictedRateBtcGate …
Run Code Online (Sandbox Code Playgroud)

javascript jquery web-worker websocket service-worker

4
推荐指数
1
解决办法
4992
查看次数