-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
94 lines (69 loc) · 1.88 KB
/
main.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
86
87
88
89
90
91
92
93
94
var {app, BrowserWindow,} = require("electron");
var path = require("path");
var url = require("url");
const { ipcMain } = require('electron')
require("electron-reload")(__dirname + "/app/index.html", {
electron: path.join(__dirname, "node_modules", ".bin", "electron")
});
app.on("ready", () => {
win = new BrowserWindow({width: 800, height: 600, webPreferences: {nodeIntegration: true}});
win.loadURL(url.format({
"pathname": path.join(__dirname, "/index.html"),
"protocol": "file:",
"slashes" : true
}));
// Emitted when the window is closed.
win.on("closed", () => {
win = null;
});
});
var myFunctionList = {};
// messages from window
ipcMain.on("Message", (event, data) => {
console.log(data)
try{
myFunctionList[data.func](data.data);
}catch(err){
console.log(`ERROR: The function "${data.func}" doesn't exist`);
}
});
// messages from server
var client = require("dgram").createSocket("udp4").bind(process.env.port);
client.on("listening", () => {
console.log(`Listening on port ${client.address().port}`);
});
client.on("message", async(message) => {
var data = JSON.parse(message.toString("utf-8"));
try{
myFunctionList[data.func](data.data);
}catch(err){
console.log(err);
}
});
// send messages to window
function SendMessage(func, data = null){
win.webContents.send("message", {
"func": func,
"data": data
});
}
// sending messages to server
function UDP(func, data = null){
var message = Buffer.from(JSON.stringify({
"func": func,
"data": data
}));
client.send(message, 0, message.length, 4000, "localhost");
}
myFunctionList.UpdateChat = (data) => {
SendMessage("UpdateChat", data);
}
myFunctionList.Message = (data) => {
UDP("Message", JSON.stringify({
"name": data.name,
"message": data.message,
}));
}
myFunctionList.ChatMessage = (data) => {
UDP("ChatMessage", data);
}