Javascript 回复webContents.send
我的理解是,您不能从呈现的进程回复从主进程发送的事件。 当通信从渲染器进程转到主进程时,可以执行相同的操作:Javascript 回复webContents.send,javascript,electron,ipc,Javascript,Electron,Ipc,我的理解是,您不能从呈现的进程回复从主进程发送的事件。 当通信从渲染器进程转到主进程时,可以执行相同的操作: // Renderer process ipcRenderer.invoke('some-name', someArgument).then((result) => { // ... }) // Main process ipcMain.handle('some-name', async (event, someArgument) => { const result
// Renderer process
ipcRenderer.invoke('some-name', someArgument).then((result) => {
// ...
})
// Main process
ipcMain.handle('some-name', async (event, someArgument) => {
const result = await doSomeWork(someArgument)
return result
})
资料来源:
对于一个简单的撤销/重做,我需要与之相反的方法,这就是我目前使用的方法:
mainProcess.js:
// somewhere in the menu template
{
id: 'undo',
label: 'Undo',
accelerator: 'Command+Z',
selector: 'undo:',
click: () => {
this.mainWindow.webContents.send('undo-request');
}
},
// down in the main process
ipcMain.on('undo-response', (_event, { canUndo }) => {
const appMenu = Menu.getApplicationMenu();
const undoMenuItem = appMenu.getMenuItemById('undo');
undoMenuItem.enabled = canUndo;
});
ipcRenderer.on('undo-request', () => {
canvas?.getCommandStack().undo();
ipcRenderer.send('undo-response', canvas?.getCommandStack().canUnod());
})
renderer.js:
// somewhere in the menu template
{
id: 'undo',
label: 'Undo',
accelerator: 'Command+Z',
selector: 'undo:',
click: () => {
this.mainWindow.webContents.send('undo-request');
}
},
// down in the main process
ipcMain.on('undo-response', (_event, { canUndo }) => {
const appMenu = Menu.getApplicationMenu();
const undoMenuItem = appMenu.getMenuItemById('undo');
undoMenuItem.enabled = canUndo;
});
ipcRenderer.on('undo-request', () => {
canvas?.getCommandStack().undo();
ipcRenderer.send('undo-response', canvas?.getCommandStack().canUnod());
})
如果主进程也可以等待承诺,则不需要额外的undo响应
事件:
// somewhere in the menu template
{
id: 'undo',
label: 'Undo',
accelerator: 'Command+Z',
selector: 'undo:',
click: () => {
this.mainWindow.webContents.send('undo-request').then(({ canUndo }) => {
const appMenu = Menu.getApplicationMenu();
const undoMenuItem = appMenu.getMenuItemById('undo');
undoMenuItem.enabled = canUndo;
});
}
},
有没有一种方法可以发送对从主进程发送的事件的某种回复?以下是一个在主进程和渲染器之间建立通信的简单示例,从主进程启动:
// Renderer process
ipcRenderer.invoke('some-name', someArgument).then((result) => {
// ...
})
// Main process
ipcMain.handle('some-name', async (event, someArgument) => {
const result = await doSomeWork(someArgument)
return result
})
main.js
// check if the window is loaded, before
// sending a data through desired communication channel
mainWindow.webContents.on('did-finish-load', () => {
// initiate the communication from
// the main process through the window object
mainWindow.webContents.send('ping', 'Message: Ping!');
});
// setup a listener to receive data sent
// through desired communication channel
// expect the data from the renderer
ipcMain.on('pong', (event, arg) => {
console.log(arg); // prints -> Message: Pong!
});
// setup a listener to receive data sent
// through desired communication channel
// expect the data from the main process
ipcRenderer.on('ping', (event, arg) => {
console.log(arg); // prints -> Message: Ping!
// send back a reply to the main process
event.sender.send('pong', 'Message: Pong!');
});
renderer.js
// check if the window is loaded, before
// sending a data through desired communication channel
mainWindow.webContents.on('did-finish-load', () => {
// initiate the communication from
// the main process through the window object
mainWindow.webContents.send('ping', 'Message: Ping!');
});
// setup a listener to receive data sent
// through desired communication channel
// expect the data from the renderer
ipcMain.on('pong', (event, arg) => {
console.log(arg); // prints -> Message: Pong!
});
// setup a listener to receive data sent
// through desired communication channel
// expect the data from the main process
ipcRenderer.on('ping', (event, arg) => {
console.log(arg); // prints -> Message: Ping!
// send back a reply to the main process
event.sender.send('pong', 'Message: Pong!');
});
电子样板文件:
我认为这目前是不可能的。我对我的应用程序也有同样的需求,但最终还是像你一样/你好!欢迎来到堆栈溢出!请更新您的答案,并解释上面的代码为什么以及如何回答问题?仅从代码中不清楚。@user230910谢谢。我希望这次更新能解开乒乓球背后的谜团。这不正是我现在正在做的吗?