Я экспериментирую на платформе Electron (атомная оболочка) для создания настольного приложения, в настоящее время на Mac OS X.Электронные сообщения IPC
Я пытаюсь подключить модуль IPC (inter processes communication) для отправки и получения сообщений синхронизации и асинхронного обмена данными между двумя основными процессами электронной обработки, основными и процессами рендеринга.
Однако с асинхронными сообщениями я получаю больше ожидаемых ответов на сообщения, отправленные из процесса рендеринга в основной процесс, на который они отвечают. Я знаю, что эта форма представляет собой консольный вывод, который должны отвечать ответы из основного процесса.
Для каждого из двух компонентов DOM я посылаю одно сообщение в основной процесс и отвечает ответом, который записывается на консоль. Но для 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 компонента '' в процессе рендеринга захватывает ответы async 2 из основного процесса и каждый из двух журналов консоли компонентов для каждого из этих двух процессов? 2 сообщения, отправленные на главный, и он произвел 2 ответа, 2 компонента в рендерере прослушивают и каждый захватывает каждый и каждый 2 журнала журналов консоли, добавляет до 4, квадрат числа компонентов прослушивания. –
У кого-нибудь есть решение этой проблемы .. – Jeevan