使用node.js os.cpus()来检测用户空闲时间? [英] Using node.js os.cpus() to detect user idle time?

查看:281
本文介绍了使用node.js os.cpus()来检测用户空闲时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a chat application with appjs that uses node.js as the platform. I'm stucked with detecting when computer is idle (when user is away from it or not using it).

node.js中有一个os模​​块,其os.cpus()为每个内核提供了此类信息:

There is os module in node.js and its os.cpus() provides such information for each core:

[ { 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 } } 
]

那么它是否适合检测用户空闲?据我了解,可以使用两个值:useridle. idle值迭代非常快,但user值却以混乱的方式迭代.我正在寻找的是要知道用户何时不移动鼠标或在ANY应用程序中键入(不仅在我的应用程序中),并且在不活动的某个阈值超时(例如60秒)后,我需要将其状态更改为离开"当他回来时,将其改回在线"状态.您能否为我指出一些算法,甚至为我删除一些代码示例? 预先感谢.

So does it fit for detecting user idle? As I understand there are two values which I can use: user and idle. The idle value iterates very quickly but the user one iterates in chaotic way. What I'm looking for is to know when user is not moving mouse or typing in ANY application (not only in my app) and after some threshold timeout (for example 60 seconds) of inactivity I need to change his status to 'away' and when he's back change it back 'to online'. Could you please point me to some alghorithm how to do that or even drop some code example for me? Thanks in advance.

编辑. 据我所知,每个操作系统都有API来检测用户是否空闲,例如Adobe Air作为平台就可以轻松地做到这一点,而且我知道我可以使用node-ffi甚至编写模块.此外,据我所知,Chromium也具有此功能

Edit. As I know each OS has API to detect user is idle and for example Adobe Air as platform has ability to do that easily and I know I can use node-ffi or even write a module. Furthermore as I know Chromium also has this ability out of the box.

推荐答案

根据os.cpus()文档:

According to os.cpus() documentation :

返回一个对象数组,其中包含有关每个CPU/内核的信息 已安装:型号,速度(以MHz为单位)和时间(包含 CPU/核心花费的毫秒数:user,nice,sys,idle, 和irq).

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窗口的系统.

So the times field shows all the time spent on a cpu core and how it was used since startup. But this is not what you are looking for because the output details CPU usage for system not for appjs window in particular.

现在如何确定您的appjs应用程序处于活动状态/空闲状态.您应该检查appjs 页面.您可以在应用程序中使用诸如mousemovekeydown之类的一些事件.

Now how to find out your appjs application is active/idle. You should check the appjs page. There are some events like mousemove and keydown which you can use in you application.

  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 os.cpus()来检测用户空闲时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆