节点隐藏模块未在Webworker上自动注册

时间:2018-07-01 19:24:13

标签: javascript electron

我通过node-hid得到了这个

  
    

未捕获的错误:模块未自注册。         在process.module。(匿名函数)[作为dlopen](ELECTRON_ASAR.js:172:20)         在Object.Module._extensions..node(module.js:671:18)         在Object.module。(匿名函数)[作为.node](ELECTRON_ASAR.js:172:20)         在Module.load(module.js:561:32)         在tryModuleLoad(module.js:504:12)         在Function.Module._load(module.js:496:3)         在Module.require(module.js:586:17)         在要求时(internal / module.js:11:18)         在绑定处(C:\ Users \ ferna \ Documents \ elantana \ Clientes \ Solid-Optics \ CodigoFuente \ WebMFTElectron \ node_modules \ bindings \ bindings.js:81:44)         在loadBinding(C:\ Users \ ferna \ Documents \ elantana \ Clientes \ Solid-Optics \ CodigoFuente \ WebMFTElectron \ node-hid \ nodehid.js:16:38)     模块。(匿名函数)@ ELECTRON_ASAR.js:172     Module._extensions..node @ module.js:671模块。(匿名函数)@     ELECTRON_ASAR.js:172 Module.load @ module.js:561 tryModuleLoad @     module.js:504 Module._load @ module.js:496 Module.require @     module.js:586要求@内部/module.js:11绑定@     C:\ Users \ ferna \ Documents \ elantana \ Clientes \ Solid-Optics \ CodigoFuente \ WebMFTElectron \ node_modules \ bindings \ bindings.js:81     loadBinding @     C:\ Users \ ferna \ Documents \ elantana \ Clientes \ Solid-Optics \ CodigoFuente \ WebMFTElectron \ node-hid \ nodehid.js:16     showdevices @     C:\ Users \ ferna \ Documents \ elantana \ Clientes \ Solid-Optics \ CodigoFuente \ WebMFTElectron \ node-hid \ nodehid.js:132     scanDevices @ mft.js:26(匿名)@ VM130:1

  

首先使用普通的javascript代码访问设备,然后使用Webworker访问设备,然后出现问题。反之亦然。

我举了一个简单的例子,只是试图检索存在的设备

这是我的JavaScript代码:

nodehid = require("node-hid")
var hiddevices = new Worker('./lib/hid/hiddevices.js');

hiddevices.addEventListener('message', function(e) {
    var result = e.data;
    switch (result.command) {
        case HID_SCAN_DEVICES:
             console.log(result);
             break;

    }
}, false);



var usbInfo = {
    vendorId: 0x10C4,
    productId: 0xEA90,
};

/**
 * Scan the devices present at the system and filters them to match only the MFT
 * @return {Message} JSON  message
 */
function scanDevices_nww() {

    var rawDevices = nodehid.devices();
    var mftDevices = rawDevices.filter(x=>x.vendorId == usbInfo.vendorId && x.productId == usbInfo.productId);
    if(mftDevices.length > 0) {
        mftInfo.usbpath = mftDevices[0].path;
    }
}


function scanDevices_WebWorker() {
    var scanDevices_WebWorkerCommand= {
        command: HID_SCAN_DEVICES,
        params: null
    }
    hiddevices.postMessage(scanDevices_WebWorkerCommand);
}

这是网络工作者的代码

if ("function" === typeof importScripts) {

importScripts("../solid-optics/commandconsts.js");
importScripts("../hid/hidreports.js");

self.addEventListener('message', function(e) {
    var command = e.data;
    var result = null;

    switch (command.command) {        
        case HID_SCAN_DEVICES:
            result = scanDevices();
            break;
            break;
        case HID_INITIALICE:
            result = initMFT(command.params);
            break;
    }

    return self.postMessage(result);

}, false);

/**
 * Scan the devices present at the system and filters them to match only the MFT
 * @return {Message} JSON  message
 */
function scanDevices() {

    var rawDevices = nodehid.devices();
    var mftDevices = rawDevices.filter(x=>x.vendorId == usbInfo.vendorId && x.productId == usbInfo.productId);
    return {
        command: HID_SCAN_DEVICES,
        result: mftDevices.length
    }
}

我在main.js上有这个

webPreferences: {
        nodeIntegrationInWorker: true
}

1 个答案:

答案 0 :(得分:0)

当您加载节点隐藏库时,它看起来被锁定在已使用的线程中。要重新使用它,您需要卸载该库并再次加载。

我决定将所有内容移入主线程,而不是使用webworkers,而是使用promises。