Events are sent through mainWindow.webContents.send in the electron main thread and cannot be received by the rendering thread

function to be implemented: click the button on the
page 1 (rendering process 1) to send the event to the main process. After the main process successfully receives the event, it sends the event event through mainWindow.webContents.send. Listen to the event, through the ipcRender.on method in page 2 (rendering process 2). When the event is received, the business processing starts.

problem:
the main process successfully received the event sent by page 1 (rendering process 1), and then sent to event event, page 2 (rendering process 2) via mainWindow.webContents.send in the main process and did not receive it.

Code:

Page 1 (rendering process 1):

const ipcRenderer = require("electron").ipcRenderer;

function playDanmu(){
    ipcRenderer.send("sigPlayDanmu", "123456");
};

main process:

const ipcMain = require("electron").ipcMain;
ipcMain.on("sigPlayDanmu", (event, args) => {
    console.log("sigPlayDanmu is captured");
    mainWindow.webContents.send("ping", "pong");
});

Page 2 (rendering process 2):

    <script>
        require("electron").ipcRenderer.on("ping", (event, message) => {
            ...
        })
    </script>

is there something wrong with the posture?

so I would like to ask you, how does the main thread actively request the rendering thread to call the method of the rendering thread or trigger the event of the rendering thread?

Thank you!

Mar.25,2021

in the main thread, the object calling the webContents.send () method is misused.
the window object is used to call messages to the rendering thread corresponding to the window.
just change the above to page2WindowObject.webContents.send ()

Menu