gpt4 book ai didi

node.js - 使用node.js os.cpus()来检测用户空闲时间?

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:37 25 4
gpt4 key购买 nike

我正在使用 appjs 开发一个聊天应用程序,该应用程序使用 node.js 作为平台。我一直在检测计算机何时处于空闲状态(当用户离开计算机或不使用计算机时)。

node.js中有一个os模​​ block ,它的os.cpus()为每个核心提供了这样的信息:

[ { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
speed: 2926,
times:
{ user: 252020,
nice: 0,
sys: 30340,
idle: 1070356870,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 306960,
nice: 0,
sys: 26980,
idle: 1071569080,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 248450,
nice: 0,
sys: 21750,
idle: 1070919370,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 256880,
nice: 0,
sys: 19430,
idle: 1070905480,
irq: 20 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 511580,
nice: 20,
sys: 40900,
idle: 1070842510,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 291660,
nice: 0,
sys: 34360,
idle: 1070888000,
irq: 10 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 308260,
nice: 0,
sys: 55410,
idle: 1071129970,
irq: 880 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 266450,
nice: 1480,
sys: 34920,
idle: 1072572010,
irq: 30 } }
]

那么它适合检测用户空闲吗?据我了解,我可以使用两个值:useridleidle 值的迭代速度非常快,但 user 值的迭代方式很困惑。我正在寻找的是知道用户何时没有移动鼠标或在任何应用程序(不仅是在我的应用程序中)中键入,并且在不活动的某个阈值超时(例如 60 秒)之后,我需要将其状态更改为“离开”当他回来时将其改回“在线”。您能否向我指出一些如何做到这一点的算法,甚至为我提供一些代码示例?提前致谢。

编辑。据我所知,每个操作系统都有 API 来检测用户是否空闲,例如 Adob​​e Air 作为平台能够轻松做到这一点,我知道我可以使用 node-ffi 甚至编写一个模块。此外据我所知Chromium也有这种能力out of the box .

最佳答案

根据os.cpus()documentation :

Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).

因此,times 字段显示了 cpu 核心上花费的所有时间以及自启动以来它的使用情况。但这不是您要寻找的内容,因为输出详细说明了系统的 CPU 使用情况,而不是特别是 appjs 窗口的 CPU 使用情况。

现在如何确定您的 appjs 应用程序是否处于事件/空闲状态。您应该检查 appjs page 。您可以在应用程序中使用一些事件,例如 mousemovekeydown

  window.addEventListener('keydown', function(e){
//if last trigger is there remove it and add new trigger
//else call makeactive()
});

window.addEventListener('mousemove', function(e){
//if last trigger is there remove it and add new trigger
//else call makeactive()
});

//To trigger timeout activity do something like this
setTimeout(function () {
//change icons, set status as away.
}, 60000); // after 60 seconds timeout

//To reset inactivity changes if it is already idle
makeactive(){
//reset icons, set status as active.
}

关于node.js - 使用node.js os.cpus()来检测用户空闲时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com