本文整理汇总了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)
}
}
})
}