-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
281 lines (209 loc) · 8.2 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* main.js */
/* Node script for the Electron client UI which also activates the Express Server */
console.log('\x1b[33m','______________________________________________________________________');
console.log('',"electron-container\n\t.");
console.log('','\tnode main.js');
console.log('','\tgit: https://github.com/mezcel/electron-container\n', '\x1b[0m');
// Note: console colors -- https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
/* ---------------------------------------------------------------------
* EXPRESS
* ------------------------------------------------------------------ */
var express = require('express');
var expressApp = require('express')();
var myHttpserver = require('http').createServer(expressApp);
var io = require('socket.io')(myHttpserver);
const fs = require('fs');
var myAppPath = __dirname + '/';
var PORT = process.env.PORT || 7777;
// expressApp.get('/', (req, res) => res.render(myAppPath + "index.html"));
expressApp.get('/', function(req, res){
res.sendFile(myAppPath + "index.html");
});
expressApp.use(express.static(myAppPath));
function getRandomHexColor(data) {
var ascii2text = data.charCodeAt(0).toString(16);
var letters = '012345'.split('');
var hexcolor = '#';
hexcolor += letters[Math.round(Math.random() * 5)];
letters = '0123456789ABCDEF'.split('');
for (var i = 0; i < 5; i++) {
hexcolor += letters[Math.round(Math.random() * 15)];
}
// cut the deck
var firstPart = "#9" + ascii2text.substr(ascii2text.length - 2);
var secondPart = hexcolor.substr(hexcolor.length - 2) + "0";
hexcolor = firstPart + secondPart;
return hexcolor;
}
function getIPvarString() {
var os = require('os');
var interfaces = os.networkInterfaces();
var addresses = [];
console.log('','Available Network Interface Address:');
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
console.log('\x1b[36m', '\t' + address.family + ' - ' + address.address, '\x1b[0m'); // debug
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
returnIp = addresses[0]; // just grab the 1st on the list
return returnIp;
}
// Domain
var sharedIpDisplay = getIPvarString();
var myHostip = "http://" + sharedIpDisplay;
if (myHostip == "http://undefined") {
// I am just going to assume 127.0.0.1
sharedIpDisplay = "localhost";
myHostip = "http://" + sharedIpDisplay;
}
console.log("\n\tServer Ip = \x1b[4m" + myHostip + '\x1b[0m');
users = [];
io.on('connection', function(socket) {
socket.on('setUsername', function(data) {
var randomHexColor = getRandomHexColor(data);
var usercount = users.length;
var isHostComputer = false;
var tmpData = data;
// if the last 4 chars of data is "HOST"
var last4letters = data.substr(data.length - 4);
if ( last4letters == "HOST" ) {
data = data + "\@" + sharedIpDisplay;
isHostComputer = true;
}
// the following applies to additional client users
if ( users.indexOf(data) > -1 ) {
socket.emit('userExists', data + ' username is allready taken! Try another username.');
// used for clients on the same server machine
if (isHostComputer) {
tmpData = tmpData.replace(/-HOST/gi, "_ServerUser");
tmpData = tmpData.replace(/HOST/gi, "_ServerUser");
}
// data = tmpData + '_' + usercount;
data = tmpData.toString() + usercount.toString() ;
users.push(data.toString());
socket.emit('userSet', {
username: data,
allusers: users,
colorname: randomHexColor,
iptitle: myHostip
});
// display updated user array in Node
console.log('\x1b[32m', '\u2713 A messaging client user was added ++++++++++++++++++++++++++++','\x1b[0m' );
console.log('\t Current User Array, users[' + usercount + "], is: ", users);
} else {
if (users.length > 0) {
// used for clients on the same server machine
if (isHostComputer) {
tmpData = tmpData.replace(/-HOST/gi, "ServerUser");
tmpData = tmpData.replace(/HOST/gi, "ServerUser");
// data = tmpData + '_' + usercount;
data = tmpData.toString() + usercount.toString() ;
}
}
users.push(data);
socket.emit('userSet', {
username: data,
allusers: users,
colorname: randomHexColor,
iptitle: myHostip
});
// display updated user array in Node
console.log('\x1b[32m', '\u2713 A messaging client user was added ++++++++++++++++++++++++++++','\x1b[0m' );
console.log('\t Current User Array, users[' + usercount + "], is: " + users);
}
});
socket.on('msg', function(data) {
io.sockets.emit('newmsg', data); //Send message to everyone
});
socket.on('removeUser', function(data) {
var usercount = users.length - 1;
if (usercount <= 0) {usercount = ''} else {usercount -= 1}
// filter remove name from array
users = users.filter(function(e) {
return e !== data
});
// display updated user array in Node
console.log('\x1b[31m', '\u2717 A client user disconnected ----------------------------','\x1b[0m'); // display updated user array in Node
console.log('\t Current User Array, users[' + usercount + '], is: ' + users);
socket.broadcast.emit('updateUserDisplay', users);
socket.emit('userRemove', users);
});
socket.on('disconnect', function() {
socket.broadcast.emit('updateUserDisplay', users);
console.log('\t - Disconnect flag from a socketio client. (socket disconnect)', '\x1b[0m');
});
});
myHttpserver.listen(PORT, () => console.log(`\tListening on \[port :${ PORT }\]\n`));
/////////////
/* ---------------------------------------------------------------------
* ELECTRON
* ------------------------------------------------------------------ */
const {
app,
BrowserWindow,
Menu,
shell
} = require('electron'); // Electron specific vars
let mainWindow;
function setMainWindowFavicon() {
var faviconPath = "";
var unpackagedPath = "myAssets/img/favicon.ico";
var packagedPath = "resources/app/myAssets/img/favicon.ico";
var verbosePath = process.cwd() + "/";
verbosePath = verbosePath + packagedPath;
if ( fs.existsSync( unpackagedPath ) ) {
faviconPath = unpackagedPath;
} else if ( fs.existsSync( packagedPath ) ) {
faviconPath = packagedPath;
} else if ( fs.existsSync( verbosePath ) ) {
faviconPath = verbosePath;
}
return faviconPath
}
function createMainWindow() {
// Create the browser window.
/* My personal settings: 700x900
* tablet reff: 768 x 1024, -25% = 576x768
* */
var faviconPath = setMainWindowFavicon(); // icon: 'myAssets/img/favicon.ico'
mainWindow = new BrowserWindow({
width: 625,
height: 850,
icon: faviconPath
});
// electron menu json
var myElectronMenu = Menu.buildFromTemplate([ { label: 'Menu', submenu: [ { label: 'Web Browser Instance', accelerator: 'CmdOrCtrl+`', click() { shell.openExternal(myHostip + ':7777/') } }, {type: 'separator'}, {role: 'minimize'}, {role: 'quit'} ] }, { label: 'View', submenu: [ {role: 'toggledevtools'}, {type: 'separator'}, {role: 'resetzoom'}, {role: 'zoomin'}, {role: 'zoomout'}, {type: 'separator'}, {role: 'togglefullscreen'} ] }, { label: 'About', submenu: [ { label: 'Github', click() { shell.openExternal('http://github.com/mezcel/') } }, { label: 'Development Wiki', click() { shell.openExternal('http://mezcel.wixsite.com/rosary') } }, { type: 'separator' }, { label: 'Wiki: Mod/Debug - BrowserWindow', click() { shell.openExternal('https://electronjs.org/docs/api/browser-window') } }, { label: 'Wiki: Mod/Debug - Menu', click() { shell.openExternal('https://electronjs.org/docs/api/menu') } } ] } ]);
Menu.setApplicationMenu(myElectronMenu);
// and load the index.html of the app.
// mainWindow.loadFile('index.html'); // electron without messaging
mainWindow.loadURL('http://localhost:7777'); // the express app
// Emitted when the window is closed.
mainWindow.on('closed', function() {
mainWindow = null;
});
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
}
/////////////
/* ---------------------------------------------------------------------
* Run
* ------------------------------------------------------------------ */
app.on('ready', function() {
createMainWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', function() {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function() {
if (mainWindow === null) {
createMainWindow();
}
});