https://github.com/serapath/electron-websocket-stream
spawn browser windows connected to the main process via websocket-stream
https://github.com/serapath/electron-websocket-stream
Last synced: about 1 year ago
JSON representation
spawn browser windows connected to the main process via websocket-stream
- Host: GitHub
- URL: https://github.com/serapath/electron-websocket-stream
- Owner: serapath
- License: mit
- Created: 2017-01-21T18:22:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-29T20:16:03.000Z (over 9 years ago)
- Last Synced: 2024-10-06T21:46:41.342Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/electron-websocket-stream
- Size: 22.5 KB
- Stars: 8
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# electron-websocket-stream
spawn browser windows connected to the main process via websocket-stream
# usage
`npm install electron-websocket-stream`
```js
var electrows = require('electron-websocket-stream')
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`
hello world
`
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
// ws.end() // closes the websocket stream & exits the BrowserifyWindow `win`
}
```