尝试通过ipc将数据从一个电子窗口发送到另一个电子窗口 [英] Trying to send data from one electron window to another via ipc

查看:62
本文介绍了尝试通过ipc将数据从一个电子窗口发送到另一个电子窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建我的第一个电子应用程序,我希望工作流程如下:

Building my first electron app and I'd like the workflow to be as follows:

mainWindow打开->用户单击打开"按钮->第二个窗口打开->用户输入输入/命中提交-> mainWindow打开备份并显示用户输入

mainWindow opens -> user clicks 'open' button -> 2nd window opens -> user enters input/hits submit -> mainWindow opens back up displaying user input

下面是我的main.js中的我的app.on('ready').应用程序启动(win.loadURL)可以正常工作,open-new-window事件也可以.怪异出现在input-broadcast中.

Below is my app.on('ready') from my main.js. The application startup (win.loadURL) works fine, as does the open-new-window event. The weirdness comes in the input-broadcast.

当用户在第二个窗口中输入输入时,主窗口将重新打开.但是,input-broadcastconsole.log中的文本不会出现,并且input-received永远不会在主窗口的渲染器中触发.

When the user enters input in the second window, the main window will re-open. However, the text in the console.log in the input-broadcast does not appear, and the input-received never fires in the main window's renderer.

不确定我做错了什么,但是我也可能使用了错误的设计模式.任何帮助将不胜感激!

Not sure what I'm doing wrong, however I may be using the wrong design pattern as well. Any help would be greatly appreciated!

main.js

const {app, BrowserWindow, ipcMain, remote} = require('electron');
const url = require('url');
const path = require('path');
const countdown = require('./countdown');

let win;
const windows = [];

app.on('ready', () =>{
console.log('app ready');

ipcMain.on('open-new-window', () =>{
    console.log('trying to open window');
        win.loadURL(url.format({
            pathname: path.join(__dirname, 'enterValue.html'),
            protocol: 'file:',
            slashes: true
        }));
});

ipcMain.on('input-broadcast', (evt, data) =>{
    console.log('input-broadcast happened in main');
    win.webContents.send('input-received', data);
    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
    }));      
});

win = new BrowserWindow({width: 800, height: 600});
win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
}));
win.on('closed', () =>{
    console.log('closed');
    win = null;
});
})

renderer.js(与index.html关联)

console.log('from renderer1');

const {ipcRenderer} = require('electron');

document.getElementById('start').addEventListener('click', ()=>{
    ipcRenderer.send('open-new-window');
    console.log('window button clicked');
});

ipcRenderer.on('open-new-window', (evt, count) =>{
    document.getElementById('userInput').innerHTML(count);
});

ipcRenderer.on('input-received', (evt, data) =>{
    console.log('input received');
    console.log(evt);
    console.log(data);
});

renderer2.js(与用户enterValue.html关联)

console.log('from renderer2');

const {ipcRenderer} = require('electron');

document.getElementById('submitButton').addEventListener('click', (evt, input)=>{
    var input = document.getElementById('randomInput').value;
    ipcRenderer.send('input-broadcast', input);
});

index.html

  <!DOCTYPE html>
  <html>
  <head>
        <meta charset="UTF-8">
        <title>Hello World!</title>
  </head>
  <body>
        <h1>Hello World!</h1>
        <p>Your input was <span id="userInput"></span><p>
        <button id="start">Open</button>
        <script>require('./renderer')</script>
  </html>

enterValue.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <p>Enter a name</p>
    <input type="text" id="randomInput" placeholder="enter a value"/>
    <button id="submitButton">Submit</button>
    <script>require('./renderer2.js')</script>
</body>
</html>

推荐答案

将输入发送回renderer.js时,您的呼叫顺序不正确.你打电话

Your call order is not right when sending input back to renderer.js. You call

win.webContents.send('input-received', data)

index.html尚未重新加载到win时!

要解决此问题,您应该交换呼叫并确保在发送ipc消息时内容已准备就绪

To fix this you should swap the calls and make sure that the content is ready when you send ipc message

ipcMain.on('input-broadcast', (evt, data) => {
    console.log('input-broadcast happened in main')
    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
    }))
    win.webContents.once('dom-ready', () => {
        win.webContents.send('input-received', data)
    })
})

这篇关于尝试通过ipc将数据从一个电子窗口发送到另一个电子窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆