-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.js
79 lines (66 loc) · 1.58 KB
/
index.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
let electron = require('electron')
let { app, BrowserWindow } = require('electron')
let { fork } = require('child_process')
let findOpenSocket = require('./find-open-socket')
let isDev = require('electron-is-dev')
let clientWin
let serverWin
let serverProcess
function createWindow(socketName) {
clientWin = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false,
preload: __dirname + '/client-preload.js'
}
})
clientWin.loadFile('client-index.html')
clientWin.webContents.on('did-finish-load', () => {
clientWin.webContents.send('set-socket', {
name: socketName
})
})
}
function createBackgroundWindow(socketName) {
const win = new BrowserWindow({
x: 500,
y: 300,
width: 700,
height: 500,
show: true,
webPreferences: {
nodeIntegration: true
}
})
win.loadURL(`file://${__dirname}/server-dev.html`)
win.webContents.on('did-finish-load', () => {
win.webContents.send('set-socket', { name: socketName })
})
serverWin = win
}
function createBackgroundProcess(socketName) {
serverProcess = fork(__dirname + '/server.js', [
'--subprocess',
app.getVersion(),
socketName
])
serverProcess.on('message', msg => {
console.log(msg)
})
}
app.on('ready', async () => {
serverSocket = await findOpenSocket()
createWindow(serverSocket)
if (isDev) {
createBackgroundWindow(serverSocket)
} else {
createBackgroundProcess(serverSocket)
}
})
app.on('before-quit', () => {
if (serverProcess) {
serverProcess.kill()
serverProcess = null
}
})