本文整理汇总了TypeScript中dotstar-node.Dotstar类的典型用法代码示例。如果您正苦于以下问题:TypeScript Dotstar类的具体用法?TypeScript Dotstar怎么用?TypeScript Dotstar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dotstar类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: parseInt
wss.on('connection', (socket, req) => {
console.log('new connection!');
// Extract the device config from URL query params
const parsed = url.parse(req.url || '');
// Parse the config values into numbers where necessary
const config = Object.entries(qs.parse(parsed.query || '')).reduce(
(accum, [k, v]: [string, any]) => ({
...accum,
[k]: Number.isSafeInteger(parseInt(v, 10)) ? parseInt(v, 10) : v,
}),
{}
);
console.log('new connection, client size is ', wss.clients.size);
if (wss.clients.size === 1) {
dotstar = Dotstar.create(config);
dotstar.setAll(0);
dotstar.sync();
console.log(dotstar && dotstar.printBuffer());
}
liveClients.add(socket);
socket.on('pong', liveClients.add.bind(liveClients, socket));
socket.on('message', (data: string = '{}') => {
if (typeof data === 'string' && data.length > 0 && data !== 'undefined') {
const { values }: { values: Array<[number, number, number]> } = JSON.parse(data);
if (dotstar && values) {
values.map(([r, g, b], i) => {
const value = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
dotstar && dotstar.set(value, i);
});
// socket.send(JSON.stringify({ values: dotstar.read(), length: dotstar.length }));
dotstar.sync();
}
}
});
socket.on('close', async (code, reason) => {
console.log(`Socket was closed with code ${code} and reason: `, reason);
if (wss.clients.size < 1 && dotstar) {
console.log('last client closed! turning stuff off');
dotstar.setAll(0);
await dotstar.sync();
console.log(dotstar.printBuffer());
dotstar = null;
}
});
});
示例2: async
socket.on('close', async (code, reason) => {
console.log(`Socket was closed with code ${code} and reason: `, reason);
if (wss.clients.size < 1 && dotstar) {
console.log('last client closed! turning stuff off');
dotstar.setAll(0);
await dotstar.sync();
console.log(dotstar.printBuffer());
dotstar = null;
}
});
示例3:
() => wss.clients.forEach(socket => {
if (!liveClients.has(socket)) socket.terminate();
liveClients.delete(socket);
socket.ping(() => {});
if (dotstar) {
console.log(dotstar.printBuffer());
}
}),