-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
85 lines (69 loc) · 2.8 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var electrows = require('./')
var options = { debug: false }
// `debug` logs some connection details and controls whether errors
// in the browser are sent to the http server for logging
electrows(options, callback)
/********************************************************************
MAIN PROCESS
`electron.BrowserifyWindow` is additionally supported and
returns a "BrowserWindow instance" that supports the
additional method: `.webContents.connectFunctionScript` with one
parameter `scriptFn`.
Calling `.webContents.connectFunctionScript(myScriptFn)` requires
`myScriptFn` to have it's first parameter named `ws` and
returns a `websocket-stream` instance.
1. The returned `ws` instance lives in the MAIN PROCESS.
2. The argument passed to the `ws` parameter of `myScriptFn`
is the `websocket-stream` instance that lives in the
RENDER PROCESS.
3. This constitutes a real time stream connection
that allows communication between the main and render process
********************************************************************/
function callback (error, electron) {
if (error) throw error
var BrowserifyWindow = electron.BrowserifyWindow
var opts = { width: 800, height: 600, show: true }
var win = BrowserifyWindow(opts)
win.loadURL('http://www.google.de')
// win.openDevTools()
var ws = win.webContents.connectFunctionScript(scriptFn)
// listen to the 'data' event
ws.on('data', function (data) {
console.log('receive data in main process:')
console.log(typeof data)
console.log (data)
})
ws.on('end', function () { console.log('end') })
ws.write('DATA2')
// ws.on('finish', function () {console.log('finish')})
// ws.on('close', function () {console.log('close')})
// ws.on('exit', function () {console.log('exit')})
// or pipe to another stream
// ws.pipe(...)
return ws // [optional] return a duplex stream
}
/********************************************************************
RENDER PROCESS
* `scriptFn` will be stringified and browserified to run in a BrowserWindow
* `ws` parameter will be passed a websocket-stream instance arguments
for communication with the main process
* ending `ws` exits the browser window
********************************************************************/
function scriptFn (ws) {
var bel = require('bel')
var element = bel`<h1> hello world </h1>`
document.body.innerHTML = ''
document.body.appendChild(element)
var data = {
title : document.title,
url : location.href,
content : document.querySelector('h1').innerText
}
ws.on('data', function (data) {
console.log('receive data in browserify window:')
console.log(data)
})
ws.write(data) // sende data to the main process
window.ws = ws
// ws.end() // closes the websocket stream & exits the BrowserifyWindow `win`
}