我有这样的代码并且有奇怪的效果, 如果有超过3个设备打开, 它无法正常工作。
我在python中使用select polling具有相同的功能 它运作良好。
请说“我哪儿坏?”
var fs = require("fs"),
path = require("path");
function is_device(device_path) {
return (
device_path.indexOf("event") >= 0 &&
fs.statSync(device_path).isCharacterDevice()
);
}
function list_devices(base_dir) {
return fs.readdirSync(base_dir).map(function(device){
return path.join(base_dir, device);
}).filter(is_device);
}
var chunks = [];
function read_devices(base_dir) {
// if we slice return of list_devices to 3
// it will be work good and many dots we can see....
var devices = list_devices(base_dir).map(function(device_name){
try {
return fs.createReadStream(device_name, {
flags: 'r',
encoding: null
})
.on('readable', function(){
this.read(16);
process.stdout.write('.');
});
} catch(e){
return null;
}
});
return devices.filter(function(device){return device != null;});
}
var device_streams = read_devices("/dev/input/");