为什么在进程之间发送消息时出现错误?

所以我试图将消息从父进程发送到子进程,代码非常简单:

const cp = require('child_process');

const bluetoothctl = cp.spawn('bluetoothctl', {
  stdio: ['ipc', 'inherit', 'inherit'],
});
const message = 'connect 5C:EB:68:30:EA:0A';
bluetoothctl.send(message, (err) => console.log(err));

setTimeout(() => bluetoothctl.kill(), 10000);

但是,由于某些原因,终端出现错误:

$ node script.js
Agent registered
[bluetooth]# "connect 5C:EB:68:30:EA:0A"
Invalid command in menu main: connect 5C:EB:68:30:EA:0A

Use "help" for a list of available commands in a menu.
Use "menu <submenu>" if you want to enter any submenu.
Use "back" if you want to return to menu main.

It says invalid command, but if I type the same command manually (connect 5C:EB:68:30:EA:0A) - it works.

I assume I am getting error because of quotes (you see "connect 5C:EB:68:30:EA:0A" in quotes). By default, when you create child process objects it has property "serialization" and it's "json". Maybe that is the reason? If so, how should I serialize messages from one process to another?

评论