当前位置: 首页>>代码示例>>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;未经允许,请勿转载。