This repository has been archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
174 lines (131 loc) · 5.29 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
/** CONFIG AND SERVER SETUP */
const SERVER_PORT = 8080;
const FS_PRIVATE_KEY = "/etc/letsencrypt/live/omeglit.com/privkey.pem";
const FS_CERTIFICATE = "/etc/letsencrypt/live/omeglit.com/cert.pem";
const FS_CACHAIN = "/etc/letsencrypt/live/omeglit.com/chain.pem";
const args = process.argv.slice(2);
console.debug('app arguments: ', args);
const express = require("express");
const expressApp = express();
let server;
if (args.includes("--help")) {
const txtHelp = " == Omeglit v2 server == \n" +
" node main.js [--help] [--with-http] [--localhost]\n" +
"Command options:\n" +
" --help Display this help \n" +
" --with-http Serve http public folder too\n" +
" --localhost Localhost mode (no SSL)";
console.info(txtHelp);
process.exit(0);
}
if (args.includes("--with-http")) {
expressApp.use(express.static('public'));
console.info("Running as a public HTTP server on port " + SERVER_PORT);
}
if (args.includes("--localhost")) {
server = require("http").Server(expressApp);
console.info("Running as a localhost configuration, no SSL");
} else {
const fs = require('fs');
if (fs.existsSync(FS_PRIVATE_KEY) && fs.existsSync(FS_CERTIFICATE) && fs.existsSync(FS_CACHAIN)) {
const https = require("https");
server = https.createServer({
key: fs.readFileSync(FS_PRIVATE_KEY),
cert: fs.readFileSync(FS_CERTIFICATE),
ca: fs.readFileSync(FS_CACHAIN)
}, expressApp);
console.info("Running as a remote server configuration");
} else {
console.error("Fatal error: check if SSL certificates files exists (" + FS_PRIVATE_KEY + ", " + FS_CERTIFICATE + ", " + FS_CACHAIN + ")");
process.exit(1);
}
}
const io = require("socket.io")(server,
{
cors: {
origin: ["https://omeglit.com", "https://www.omeglit.com"],
methods: ["GET", "POST"]
}
});
/** GLOBAL CHAT VARIABLES */
let lonelyClientTxt = {};
let allClientsTxt = {};
let lonelyClientVideo = {};
let allClientsVideo = {};
let lonelyClientTxt18 = {};
let allClientsTxt18 = {};
let lonelyClientVideo18 = {};
let allClientsVideo18 = {};
function countAllUsers() {
return Object.keys(allClientsTxt).length + Object.keys(allClientsVideo).length + Object.keys(allClientsTxt18).length + Object.keys(allClientsVideo18).length;
}
function emitUserCount() {
io.emit("nusers", {nusers: countAllUsers(), txtUsers: Object.keys(allClientsTxt).length, txt18Users: Object.keys(allClientsTxt18).length, videoUsers: Object.keys(allClientsVideo).length, video18Users: Object.keys(allClientsVideo18).length});
}
io.on('connection', function (socket) {
emitUserCount();
});
newOmeglit("/txt", lonelyClientTxt, allClientsTxt);
newOmeglit("/video", lonelyClientVideo, allClientsVideo);
newOmeglit("/txt18", lonelyClientTxt18, allClientsTxt18);
newOmeglit("/video18", lonelyClientVideo18, allClientsVideo18);
function newOmeglit(url, lonely, allClients) {
const ioPath = io.of(url);
ioPath.on('connection', function (socket) {
socket.on('disconnect', function () {
if (allClients[socket.id]) {
if (lonely.id === socket.id) {
lonely = {};
}
// if (allClients[socket.id].partner) {
// io.to(allClients[socket.id].partner).emit('disconnect'); //this throws Error: "disconnect" is a reserved event name
// }
delete allClients[socket.id];
emitUserCount();
} else {
console.log('A user that never registered left');
}
});
socket.on('newUser', function () {
allClients[socket.id] = socket;
//console.log('New user looking for a partner: ', socket.id);
if (lonely.id) {
//console.log(lonely.id, ' emparejado con ', socket.id);
socket.partner = lonely.id;
allClients[lonely.id].partner = socket.id;
allClients[socket.id].partner = lonely.id;
allClients[socket.id].emit('match', {
id: lonely.id,
itsok: true
});
allClients[lonely.id].emit('match', {
id: socket.id
});
lonely = {};
} else {
//console.log(socket.id, ' busca partner.');
lonely.id = socket.id;
}
//console.log(countAllUsers() + ' users online')
emitUserCount();
});
socket.on('newMessage', function (data) {
// console.log(data);
try {
if (allClients[socket.id] !== undefined && allClients[socket.id].partner) {
if (allClients[allClients[socket.id].partner] !== undefined) {
allClients[allClients[socket.id].partner].emit('newMessage', data);
}
} else {
io.to(socket.id).emit('aborted');
}
} catch (error) {
const datetime = new Date();
console.error(datetime + ": " + error);
}
});
});
}
server.listen(SERVER_PORT, "0.0.0.0", function () {
console.log("Server started on port " + SERVER_PORT);
});