📝 Update docs of ipc module.
This commit is contained in:
parent
1fd8deaca7
commit
85f6edb815
2 changed files with 47 additions and 111 deletions
|
@ -5,75 +5,18 @@ asynchronous messages to the browser, and also receive messages sent from
|
|||
browser. If you want to make use of modules of browser from renderer, you
|
||||
might consider using the [remote](remote.md) module.
|
||||
|
||||
An example of echoing messages between browser and renderer:
|
||||
See [ipc (browser)](../browser/ipc-browser.md) for examples.
|
||||
|
||||
```javascript
|
||||
// In browser:
|
||||
var ipc = require('ipc');
|
||||
ipc.on('message', function(processId, routingId, m) {
|
||||
ipc.send(processId, routingId, m);
|
||||
});
|
||||
```
|
||||
## ipc.send(channel[, args...])
|
||||
|
||||
```javascript
|
||||
// In renderer:
|
||||
var ipc = require('ipc');
|
||||
ipc.on('message', function(m) {
|
||||
console.log('Received message', m);
|
||||
});
|
||||
ipc.send('Hello world');
|
||||
```
|
||||
Send `args..` to the web page via `channel` in asynchronous message, the browser
|
||||
process can handle it by listening to the `channel` event of `ipc` module.
|
||||
|
||||
An example of sending synchronous message from renderer to browser:
|
||||
## ipc.sendSync(channel[, args...])
|
||||
|
||||
```javascript
|
||||
// In browser:
|
||||
var ipc = require('ipc');
|
||||
ipc.on('browser-data-request', function(event, processId, routingId, message) {
|
||||
event.returnValue = 'THIS SOME DATA FROM THE BROWSER';
|
||||
});
|
||||
```
|
||||
|
||||
```javascript
|
||||
// In renderer:
|
||||
var ipc = require('ipc');
|
||||
console.log(ipc.sendChannelSync('browser-data-request', 'THIS IS FROM THE RENDERER'));
|
||||
```
|
||||
|
||||
## Event: 'message'
|
||||
|
||||
Emitted when browser sent a message to this window.
|
||||
|
||||
## ipc.send([args...])
|
||||
|
||||
Send all arguments to the browser and return immediately, the browser should
|
||||
handle the message by listening to the `message` event.
|
||||
|
||||
## ipc.sendSync([args...])
|
||||
|
||||
Send all arguments to the browser synchronously, and returns the result sent
|
||||
from browser. The browser should handle the message by listening to the
|
||||
`sync-message` event.
|
||||
Send `args..` to the web page via `channel` in synchronous message, and returns
|
||||
the result sent from browser. The browser process can handle it by listening to
|
||||
the `channel` event of `ipc` module, and returns by setting `event.returnValue`.
|
||||
|
||||
**Note:** Usually developers should never use this API, since sending
|
||||
synchronous message would block the browser.
|
||||
|
||||
## ipc.sendChannel(channel, [args...])
|
||||
|
||||
* `channel` String
|
||||
|
||||
This is the same with `ipc.send`, except that the browser should listen to the
|
||||
`channel` event. The `ipc.send(args...)` can be seen as
|
||||
`ipc.sendChannel('message', args...)`.
|
||||
|
||||
|
||||
## ipc.sendChannelSync(channel, [args...])
|
||||
|
||||
* `channel` String
|
||||
|
||||
This is the same with `ipc.sendSync`, except that the browser should listen to
|
||||
the `channel` event. The `ipc.sendSync(args...)` can be seen as
|
||||
`ipc.sendChannelSync('sync-message', args...)`.
|
||||
|
||||
**Note:** Usually developers should never use this API, since sending
|
||||
synchronous message would block the browser.
|
||||
synchronous message would block the whole web page.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue