2016-09-14 8 views
2

私はElectrons同期と非同期RPC通信メカニズムを使用しており、プロセス間でデータをうまくやりとりすることができます。しかし、今では、イベントデータ(チャットアプリケーションのようなビット)をレンダラープロセスに継続的に送信し、テキストを更新する必要があります。メインからレンダラープロセスへのイベントを連続的に渡します

これは電子内で可能ですか?私は、レンダラープロセスで何らかのリスナーを作成する必要があると推測しています。

答えて

0

ipcMainipcRendererを使用できます。

メインプロセスです。

const {ipcMain} = require('electron') 
ipcMain.on('asynchronous-message', (event, arg) => { 
    event.sender.send('asynchronous-reply', 'example message...') 
}) 

レンダラープロセス(Webページ)。

const {ipcRenderer} = require('electron') 

ipcRenderer.on('asynchronous-reply', (event, arg) => { 
    // arg contain your message (example message...) 
}) 
ipcRenderer.send('asynchronous-message', 'example example send to main process') 

任意のオブジェクトを渡すこともできます。

+0

感謝。これは、メインプロセスがそれらを送信している限り、レンダラプロセスでイベントを継続的に受信しますか? – user1513388

+0

はストリームが好きですか? – MaximeF

+0

はい - これはまさに私の意味です。 – user1513388

0

そうです。例えば

メインプロセス:

const ipc = require('electron').ipcMain 

ipc.on('asynchronous-message', function (event, arg) { 
event.sender.send('asynchronous-reply', 'pong') 

function countdown(elementName, minutes, seconds) 
{ 
var element, endTime, hours, mins, msLeft, time; 

function twoDigits(n) 
{ 
    return (n <= 9 ? "0" + n : n); 
} 

function updateTimer() 
{ 
    msLeft = endTime - (+new Date); 
    if (msLeft < 1000) { 
    //element.innerHTML = "countdown's over!"; 
    event.sender.send('asynchronous-reply', 'countdown is over') 
    } else { 
    time = new Date(msLeft); 
    hours = time.getUTCHours(); 
    mins = time.getUTCMinutes(); 
    // element.innerHTML = (hours ? hours + ':' + twoDigits(mins) : mins) + ':' + twoDigits(time.getUTCSeconds()); 
    event.sender.send('asynchronous-reply', (hours ? hours + ':' + twoDigits(mins) : mins) + ':' + twoDigits(time.getUTCSeconds())); 
    setTimeout(updateTimer, time.getUTCMilliseconds() + 500); 
    } 
} 

// element = document.getElementById(elementName); 
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500; 
updateTimer(); 
} 
countdown("countdown", 1, 5); 
}) 

レンダラプロセス:

const {ipcRenderer} = require('electron') 

ipcRenderer.on('asynchronous-reply', (event, arg) => { 
// arg contain your message (example message...) 
}) 
ipcRenderer.send('asynchronous-message', 'example example send to main process') 
関連する問題