2015-10-06 94 views
1

我正在试验用于构建桌面应用程序的Electron(atom shell)平台,目前在mac os x上。Electron IPC过多的消息

我正在尝试它的IPC(进程间通信)模块,用于在两个主要电子进程,main和渲染器进程之间发送和接收同步和异步消息。

但是,对于异步消息,我得到的消息比预期的回复已经从渲染器进程发送到主进程,并在其中进行回复。我知道这种形式的控制台输出主进程应该产生的回复。

对于DOM的2个组件中的每一个,我向主进程发送1个单个消息,并且它回应一个应答并记录到控制台。但对于2个组件(反应组件),我得到4个控制台日志行,3个为9,而4个则为16个控制台日志消息行。

这是怎么回事?我错过了有关异步消息和IPC回复的消息?同步消息和回复没有问题。

main.js(主处理代码):

var app = require('app'); // Module to control application life. 
var BrowserWindow = require('browser-window'); // Module to create native browser window. 
var ipc = require('ipc'); 

// Report crashes to our server. 
require('crash-reporter').start(); 

// Keep a global reference of the window object, if you don't, the window will 
// be closed automatically when the JavaScript object is GCed. 
var mainAppWindow = null; 
var bookWindow = null; 

// Quit when all windows are closed. 
app.on('window-all-closed', function() { 
    // On OS X it is common for applications and their menu bar 
    // to stay active until the user quits explicitly with Cmd + Q 
    app.quit(); 
}); 

// This method will be called when Electron has finished 
// initialization and is ready to create browser windows. 
app.on('ready', function() { 
    // Create the browser window. 
    mainAppWindow = new BrowserWindow({width: 1200, height: 900, 'title-bar-style': 'hidden'}); 

    // and load the index.html of the app. 
    mainAppWindow.loadUrl('file://' + __dirname + '/index.html'); 

    mainAppWindow.openDevTools(); 
    // Emitted when the window is closed. 
    mainAppWindow.on('closed', function() { 
    // Dereference the window object, usually you would store windows 
    // in an array if your app supports multi windows, this is the time 
    // when you should delete the corresponding element. 
    mainAppWindow = null; 
    app.quit(); 
    }); 

    ipc.on('asynchronous-message', function(event, arg) { 
    console.log(arg); // prints "ping" 
    event.sender.send('asynchronous-reply', 'pong'); 
    }); 


    // listen for the messages from renderer process to close the mainwindow and open a readerwindow 
    ipc.on('synchronous-message', function(event, arg) { 
    console.log(arg); // prints "ping" 
    event.returnValue = 'pong'; 
    }); 
    // when all of the book reader windows are closed open the mainAppWindow 
    // in main process listen for all bookreaderwindows are closed 
    // and in each, checking an array of them formed when they are each created. 
}); 

index.js(渲染处理代码):

/* 
-mainApp 
    -Controls 
    -Books 
    -Book 
*/ 
var ipc = require('ipc'); 

var Book = React.createClass({ 
    switchToBookReader: function() { 
    // close mainAppWindow, and open bookreader window. 
    // send message to main process 'close window'. 
    // listen in main process for this. 
    // callback for this in main process is to close the main window and open a readerwindow 
    }, 
    componentDidMount: function() { 
    ipc.send('asynchronous-message', 'ping'); 
    ipc.on('asynchronous-reply', function(arg) { 
     console.log(arg); // prints "pong" 
    }); 
    console.log(ipc.sendSync('synchronous-message', 'ping')); // prints "pong" 
    }, 
    render: function() { 
    return (
     <div onDoubleClick={this.switchToBookReader()} > 
     {this.props.name} 
     </div> 
    ); 
    } 
}); 

var Books = React.createClass({ 
    render: function() { 
    // create Book nodes here. 
    var bookNodes = []; 
    bookNodes.push(<Book name="book1"/>); 
    bookNodes.push(<Book name="book2"/>); 


    return (
     <div> 
     {bookNodes} 
     </div> 
    ); 
    } 
}); 

var Controls = React.createClass({ 
    render: function() { 
    return (
     <div> 
     Controls 
     </div> 
    ); 
    } 
}); 

var mainApp = React.createClass({ 
    render: function() { 
    return (
     <div> 
     <Controls /> 
     <Books /> 
     </div> 
    ); 
    } 
}); 

React.render(<mainApp />, document.body); 
+0

等等!在渲染器进程中,2个侦听的''组件是否捕获来自主进程的异步2响应,并且这两个组件中的每一个都为这两个进程中的每一个控制台记录? 2消息发送到主,它发射了2个回复,渲染器中的2个组件侦听并且每个捕获每个和每个2个日志2个控制台日志,最多为4个监听组件的数量的平方。 –

+0

有人有解决这个问题.. – Jeevan

回答