我正在試驗用於構建桌面應用程序的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);
等等!在渲染器進程中,2個偵聽的''組件是否捕獲來自主進程的異步2響應,並且這兩個組件中的每一個都爲這兩個進程中的每一個控制檯記錄? 2消息發送到主,它發射了2個回覆,渲染器中的2個組件偵聽並且每個捕獲每個和每個2個日誌2個控制檯日誌,最多爲4個監聽組件的數量的平方。 –
有人有解決這個問題.. – Jeevan