Electron回复错误:无法克隆对象

The*_* xd 3 ipc electron

我试图要求主应用程序找到某种设备,我一直在尝试使用 ipc 来做到这一点,但无法使其与异步发送和同步发送一起工作。我怀疑主要是在试图回应诺言。

渲染器:

const recognizedDevices = ipcRenderer.sendSync('findDevice');
console.log(recognizedDevices);
Run Code Online (Sandbox Code Playgroud)

主要的:

ipcMain.on('findDevice', (event) => findDevice(event));
const findDevice = async (event) => {
  let recognizedDevices = await findConnectedDevices();
  if(recognizedDevices){
  console.log("found");
  console.log(recognizedDevices);
  return event.returnValue = recognizedDevices;
  }
//TODO: If no device found
}
Run Code Online (Sandbox Code Playgroud)

主要结果:

    found
[
  HID {
    _events: [Object: null prototype] { newListener: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    _raw: HID {},
    write: [Function: bound write],
    getFeatureReport: [Function: bound getFeatureReport],
    sendFeatureReport: [Function: bound sendFeatureReport],
    setNonBlocking: [Function: bound setNonBlocking],
    readSync: [Function: bound readSync],
    readTimeout: [Function: bound readTimeout],
    getDeviceInfo: [Function: bound getDeviceInfo],
    _paused: true,
    [Symbol(kCapture)]: false
  }
]
Run Code Online (Sandbox Code Playgroud)

我希望在渲染器中收到相同的日志结果,但不幸的是我回来了Error: An object could not be cloned.

如果我尝试用 recognizeDevice.length 回复,我确实会在前端收到“1”,所以看起来它们之间的通信良好。问题似乎出在发送对象上。

cus*_*der 5

主进程和渲染进程之间通过 IPC 通道交换的数据通过结构化克隆算法进行序列化。函数和符号不可序列化,但您正尝试将它们传送过来:

\n
 [\n   HID {\n\xcb\x9f   _events: [Object: null prototype] { newListener: [Function (anonymous)] },\n    _eventsCount: 1,\n    _maxListeners: undefined,\n    _raw: HID {},\n\xcb\x9f   write: [Function: bound write],\n\xcb\x9f   getFeatureReport: [Function: bound getFeatureReport],\n\xcb\x9f   sendFeatureReport: [Function: bound sendFeatureReport],\n\xcb\x9f   setNonBlocking: [Function: bound setNonBlocking],\n\xcb\x9f   readSync: [Function: bound readSync],\n\xcb\x9f   readTimeout: [Function: bound readTimeout],\n\xcb\x9f   getDeviceInfo: [Function: bound getDeviceInfo],\n    _paused: true,\n\xcb\x9f   [Symbol(kCapture)]: false\n   }\n ]\n
Run Code Online (Sandbox Code Playgroud)\n