-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (61 loc) · 1.7 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
const { app, BrowserWindow, ipcMain } = require('electron');
const WebSocket = require('ws');
const WEB_SOCKET_PORT = 9990;
const COMMAND_KEY = "quad-command";
const CONNECT_STATUS_KEY = "conn-status";
const wss = new WebSocket.Server({port: WEB_SOCKET_PORT});
let window;
let DEBUG = true;
// Recieve Message on WebSocket from Client
wss.on('connection', ws => {
ws.on('message', message => {
Log(`Received: ${message}`);
window.webContents.send(CONNECT_STATUS_KEY, true);
// Send recieved message to the renderer window
window.webContents.send(COMMAND_KEY, String(message));
});
ws.on("close", (reasonCode, desc) =>
{
window.webContents.send(CONNECT_STATUS_KEY, false);
});
});
// Recieve Response from Renderer to Send to Client
ipcMain.on('synchronous-message', (event, arg) => {
Log("Responding: " + String(arg));
event.returnValue = 'recieved';
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(arg);
}});
});
function CreateWindow ()
{
// Create Browser Window
window = new BrowserWindow({
width: 800,
height: 800,
webPreferences: {
nodeIntegration: true
}
});
window.setMenuBarVisibility(false);
// and load renderer webpage
window.loadFile("renderer/index.html");
window.webContents.on('crashed', (e) => {
Log(e);
app.quit()
});
}
function Log(message)
{
if(DEBUG)
{
console.log("[LOG]", message);
}
}
app.on('ready', CreateWindow);
app.on('quit', () => {
Log("Cleaning Up...");
wss.close();
app.quit()
});