Skip to content

Commit bed3782

Browse files
committed
small clean up
1 parent 23401ad commit bed3782

4 files changed

Lines changed: 50 additions & 106 deletions

File tree

functions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function start(io, rooms, numusers, devv) {
118118
let extraData;
119119

120120
socket.on("disconnect", () => {
121+
updateusers();
121122
if (room === null) return;
122123
if (extraData.userid === room.owner.userid) {
123124
room.terminate();

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.js

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ const path = require('path');
55
const app = express();
66
const os = require('os');
77
const netplay = require("./functions.js");
8+
const e = require('express');
89
let interfaces = os.networkInterfaces();
910
let mainserver = true;
1011
let addresses = [];
11-
let nofusers = {num: 3};
12+
let nofusers = {num: 0};
1213
let port;
1314
let password;
1415
let dev;
@@ -43,68 +44,36 @@ function startserver() {
4344
res.sendStatus(401)
4445
}
4546
});
46-
app.post('/status', (req, res) => {
47+
app.post('/api', (req, res) => {
4748
const reject = () => {
4849
res.setHeader('www-authenticate', 'Basic')
4950
res.sendStatus(401)
5051
}
5152
if (!checkAuth(req.headers.authorization, password)) {
5253
return reject();
5354
}
54-
res.end('{ "port": ' + port + ', "password": "' + password + '", "nofusers": ' + nofusers.num + ' }');
55-
});
56-
app.post('/interface', (req, res) => {
57-
const reject = () => {
58-
res.setHeader('www-authenticate', 'Basic')
59-
res.sendStatus(401)
60-
}
61-
if (!checkAuth(req.headers.authorization, password)) {
62-
return reject();
63-
}
64-
for (let k in interfaces) {
65-
for (let k2 in interfaces[k]) {
66-
let address = interfaces[k][k2];
67-
if (address.family === 'IPv4') {
68-
addresses.push("http://"+address.address+":"+port+"/");
55+
if (req.body.function === "status") {
56+
res.end('{ "status": ' + mainserver.toString() + ', "port": ' + port + ', "password": "' + password + '", "nofusers": ' + nofusers.num + ' }');
57+
}else if(req.body.function === "interface"){
58+
for (let k in interfaces) {
59+
for (let k2 in interfaces[k]) {
60+
let address = interfaces[k][k2];
61+
if (address.family === 'IPv4') {
62+
addresses.push("http://"+address.address+":"+port+"/");
63+
}
6964
}
7065
}
71-
}
72-
addresses.push("http://localhost:"+port+"/");
73-
res.end('{ "interfaces": ' + JSON.stringify(addresses) + ' }');
74-
});
75-
app.post('/check', (req, res) => {
76-
const reject = () => {
77-
res.setHeader('www-authenticate', 'Basic')
78-
res.sendStatus(401)
79-
}
80-
if (!checkAuth(req.headers.authorization, password)) {
81-
return reject();
82-
}
83-
res.end(mainserver.toString());
84-
});
85-
app.post('/numusers', (req, res) => {
86-
const reject = () => {
87-
res.setHeader('www-authenticate', 'Basic')
88-
res.sendStatus(401)
89-
}
90-
if (!checkAuth(req.headers.authorization, password)) {
91-
return reject();
92-
}
93-
res.end('{ "users": ' + nofusers.num + " }");
94-
});
95-
app.post('/startstop', (req, res) => {
96-
const reject = () => {
97-
res.setHeader('www-authenticate', 'Basic');
98-
res.sendStatus(401);
99-
}
100-
if (!checkAuth(req.headers.authorization, password)) {
101-
return reject();
102-
}
103-
if (req.body.function === "stop") {
66+
addresses.push("http://localhost:"+port+"/");
67+
res.end('{ "interfaces": ' + JSON.stringify(addresses) + ' }');
68+
}else if(req.body.function === "check"){
69+
res.end(mainserver.toString());
70+
}else if(req.body.function === "nofusers"){
71+
res.end('{ "users": ' + nofusers.num + " }");
72+
}else if(req.body.function === "stop"){
10473
mainserver = false;
10574
res.end('true');
10675
stopnetplay();
107-
} else {
76+
}else if(req.body.function === "start"){
10877
mainserver = true;
10978
res.end('true');
11079
startnetplay();

src/index.html

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,25 @@ <h1>EmulatorJS Netplay Server</h1>
146146

147147
(function() {
148148
check();
149-
status().then(info => {
150-
document.getElementById('info').innerHTML = "Running server on port " + (info.port || 3000) + " with password " + info.password;
151-
});
152-
interface().then(address => {
153-
address.interfaces.push(window.location.protocol+"//"+window.location.hostname+':'+window.location.port+'/');
149+
api("interface").then(address => {
150+
if(window.location.port != ""){
151+
address.interfaces.push(window.location.protocol+"//"+window.location.hostname+':'+window.location.port+'/');
152+
}else{
153+
address.interfaces.push(window.location.protocol+"//"+window.location.hostname+'/');
154+
}
154155
address.interfaces = [...new Set(address.interfaces)];
155156
address.interfaces.forEach(address => {
156157
document.getElementById('urls').innerHTML += '<li><a href="'+address+'" target="_blank" onclick="window.api.openExternal(this.href);event.preventDefault()">'+address+'</a></li>';
157158
});
158159
});
159-
checkforusers();
160+
statuscheck();
160161
})();
161-
162-
function status() {
163-
return fetch('/status', {
164-
method: 'POST',
165-
headers: {
166-
'Content-Type': 'application/json'
167-
}
168-
})
169-
.then(response => response.json())
170-
.then(data => {
171-
return data;
172-
});
173-
}
174-
175-
function interface() {
176-
return fetch('/interface', {
162+
function api(type) {
163+
return fetch('/api', {
177164
method: 'POST',
165+
body: JSON.stringify({
166+
function: type,
167+
}),
178168
headers: {
179169
'Content-Type': 'application/json'
180170
}
@@ -185,16 +175,10 @@ <h1>EmulatorJS Netplay Server</h1>
185175
});
186176
}
187177
function check(){
188-
fetch('/check', {
189-
method: 'POST',
190-
headers: {
191-
'Content-Type': 'application/json'
192-
}
193-
}).then(response => response.json())
194-
.then(data => {
195-
if(data == true){
178+
api("check").then(status => {
179+
if(status == true){
196180
startstop.innerText = 'Stop';
197-
}else if(data == false){
181+
}else if(status == false){
198182
startstop.innerText = 'Start';
199183
}
200184
update();
@@ -220,16 +204,7 @@ <h1>EmulatorJS Netplay Server</h1>
220204
update();
221205
}
222206
function startstopserver(option){
223-
fetch('/startstop', {
224-
method: 'POST',
225-
headers: {
226-
'Content-Type': 'application/json'
227-
},
228-
body: JSON.stringify({
229-
function: option,
230-
})
231-
}).then(response => response.json())
232-
.then(data => {
207+
api(option).then(data => {
233208
if(option == "start"){
234209
if(data == true){
235210
startstop.innerText = 'Stop';
@@ -249,20 +224,19 @@ <h1>EmulatorJS Netplay Server</h1>
249224
});
250225
}
251226
setInterval(function(){
252-
checkforusers();
227+
statuscheck();
253228
}, 5000);
254-
function checkforusers(){
255-
fetch('/numusers', {
256-
method: 'POST',
257-
headers: {
258-
'Content-Type': 'application/json',
259-
'Accept': 'application/json'
229+
function statuscheck(){
230+
api("status").then(data => {
231+
if(data.status == true){
232+
startstop.innerText = 'Stop';
233+
}else if(data.status == false){
234+
startstop.innerText = 'Start';
260235
}
261-
}).then(response => response.json())
262-
.then(data => {
263-
data = data.users;
264-
document.getElementById('nuser').innerHTML = "Users connected: "+data;
265-
});
236+
document.getElementById('nuser').innerHTML = "Users connected: "+data.nofusers;
237+
document.getElementById('info').innerHTML = "Running server on port " + (data.port || 3000) + " with password " + data.password;
238+
update();
239+
});
266240
}
267241
</script>
268242
</body>

0 commit comments

Comments
 (0)