Skip to content

Commit b8d052a

Browse files
committed
100% done with the basic server
1 parent a306ebd commit b8d052a

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
},
3030
"scripts": {
31-
"start": "node index.js ",
31+
"start": "node index.js",
3232
"dev": "node index.js -d",
3333
"app": "electron .",
3434
"build-win": "electron-builder --win --publish=never",

server.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const http = require('http');
22
const express = require('express');
3-
const Server = require('socket.io');
3+
const { Server } = require('socket.io');
44
const path = require('path');
55
const app = express();
66
const os = require('os');
7-
const { get } = require('https');
87
let interfaces = os.networkInterfaces();
98
let mainserver = true;
109
let addresses = [];
@@ -14,6 +13,7 @@ let password;
1413
let dev;
1514
let appserver;
1615
let server;
16+
let io;
1717

1818
function checkAuth(authorization, passwordforserver) {
1919
if (!authorization) return false;
@@ -91,14 +91,51 @@ function startserver() {
9191
}
9292
res.end('{ "users": ' + nofusers + " }");
9393
});
94+
app.post('/startstop', (req, res) => {
95+
const reject = () => {
96+
res.setHeader('www-authenticate', 'Basic');
97+
res.sendStatus(401);
98+
}
99+
if (!checkAuth(req.headers.authorization, password)) {
100+
return reject();
101+
}
102+
if (req.body.function === "stop") {
103+
mainserver = false;
104+
res.end('true');
105+
stopnetplay();
106+
} else {
107+
mainserver = true;
108+
res.end('true');
109+
startnetplay();
110+
}
111+
});
94112
server.listen(port || 3000, '0.0.0.0', () => {
95-
consolelog("Starting server on port " + (port || 3000) + " with password " + password);
96113
if(appserver){
97114
process.send({ function: 'url', url: 'http://localhost:' + port});
98115
}
116+
if(mainserver){
117+
startnetplay();
118+
}
119+
});
120+
}
121+
122+
function startnetplay(){
123+
consolelog("Starting Netplay");
124+
io = new Server(server, {
125+
cors: {
126+
origin: "*",
127+
methods: ["GET", "POST"],
128+
credentials: true
129+
}
99130
});
100131
}
101132

133+
function stopnetplay(){
134+
consolelog("Stopping Netplay");
135+
io.close();
136+
startserver();
137+
}
138+
102139
function consolelog(message){
103140
if(dev){
104141
console.log(message);
@@ -113,6 +150,7 @@ process.on('message', function(m) {
113150
dev = m.dev;
114151
appserver = m.app;
115152
startserver();
153+
consolelog("Starting server on port " + (port || 3000) + " with password " + password);
116154
}else if(m.function == 'kill'){
117155
process.exit();
118156
}

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ <h1>EmulatorJS Netplay Server</h1>
245245
}else{
246246
console.error("Error!");
247247
}
248+
check();
248249
});
249250
}
250251
setInterval(function(){

0 commit comments

Comments
 (0)