當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Server.on方法代碼示例

本文整理匯總了TypeScript中duniter/server.Server.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server.on方法的具體用法?TypeScript Server.on怎麽用?TypeScript Server.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在duniter/server.Server的用法示例。


在下文中一共展示了Server.on方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: listenWebSocket

function listenWebSocket(httpServer: any, duniterServer: Server, payperblock: number) {


  let remuWallet = wallet(duniterServer, payperblock);
  let lastBlocks: DBBlock[] = [];
  let top1 = remuWallet.lastDayTheoretical();
  let top2 = remuWallet.lastWeekTheoretical();
  let topgifts = remuWallet.topGifts();
  let wssBlock = new WebSocketServer({
    server: httpServer,
    path: '/ws/block'
  });

  let getData = () => co(function *() {
    try {
      let current = yield remuWallet.getCurrentBlock();
      let data1 = yield top1;
      let data2 = yield top2;
      let data3 = yield topgifts;
      yield lastBlocks.map((b: any) => co(function *() {
        const idty = yield remuWallet.getUID(b.issuer)
        b.uid = (idty && idty.uid) || b.issuer.substr(0, 10)
        return b;
      }));
      (current as any).uid = (yield remuWallet.getUID(current.issuer)).uid;
      let remains = yield remuWallet.remains();
      return {
        key: remuWallet.pubkey,
        current,
        issuersCount: current.issuersCount,
        issuersWeek: data2.length,
        issuersFrame: current.issuersFrame,
        remains: remains,
        remains_days: (Math.floor(remains / (remuWallet.pay_per_block * Math.pow(10, current.unitbase))) * duniterServer.conf.avgGenTime / (3600 * 24)).toFixed(1),
        pay_per_block: remuWallet.pay_per_block,
        unitbase: current.unitbase,
        blocks: lastBlocks,
        top1: data1,
        top2: data2,
        topgifts: data3,
        unit: PAY_UNIT
      };
    } catch (e) {
      console.error(e);
    }
  });

  wssBlock.on('connection', function connection(ws: any) {
    co(function *() {
      if (lastBlocks.length < 10) {
        let current = yield duniterServer.dal.getCurrentBlockOrNull();
        lastBlocks = yield duniterServer.dal.getBlocksBetween(current.number - 10, current.number);
      }

      ws.on('message', function(message: any) {
        console.log('received: %s', message);
        co(function *() {
          let toSend = yield getData();
          ws.send(JSON.stringify(toSend));
        });
      });
    });
  });

  wssBlock.broadcast = (data: any) => wssBlock.clients.forEach((client: any) => client.send(data));

  // Forward blocks
  duniterServer.on('data', async (data: any) => {

    if (data.bcEvent === 'newHEAD') {
      try {
        // Broadcast block
        lastBlocks.shift();
        lastBlocks.push(data.block);
        top1 = remuWallet.lastDayTheoretical();
        top2 = remuWallet.lastWeekTheoretical();
        topgifts = remuWallet.topGifts();
        co(function* () {
          let toSend = yield getData()
          wssBlock.broadcast(JSON.stringify(toSend));
        });
      } catch (e) {
        console.error(e)
      }
    }
  })
}
開發者ID:duniter,項目名稱:remuniter,代碼行數:87,代碼來源:webserver.ts


注:本文中的duniter/server.Server.on方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。