Skip to content

Commit eff7845

Browse files
committed
99% done with the basic server
1 parent fdd5d91 commit eff7845

3 files changed

Lines changed: 123 additions & 52 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"start": "node index.js",
4242
"dev": "node index.js -d",
4343
"app": "electron .",
44-
"build-win": "electron-builder --win --publish=never && pkg ./ --targets latest-win-x64 --no-bytecode",
45-
"build-linux": "electron-builder --linux --publish=never && pkg ./ --targets latest-linux-x64 --no-bytecode",
44+
"build-win": "electron-builder --win --publish=never && pkg ./ --targets latest-win-x64",
45+
"build-linux": "electron-builder --linux --publish=never && pkg ./ --targets latest-linux-x64",
4646
"build-docker": "docker build -t emulatorjs-netplay-server ."
4747
},
4848
"dependencies": {

server.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ const express = require('express');
33
const Server = require('socket.io');
44
const path = require('path');
55
const app = express();
6+
const os = require('os');
7+
const { get } = require('https');
8+
let interfaces = os.networkInterfaces();
9+
let mainserver = true;
10+
let addresses = [];
611
let nofusers = 0;
712
let port;
813
let password;
@@ -47,6 +52,45 @@ function startserver() {
4752
}
4853
res.end('{ "port": ' + port + ', "password": "' + password + '", "nofusers": ' + nofusers + ' }');
4954
});
55+
app.post('/interface', (req, res) => {
56+
const reject = () => {
57+
res.setHeader('www-authenticate', 'Basic')
58+
res.sendStatus(401)
59+
}
60+
if (!checkAuth(req.headers.authorization, password)) {
61+
return reject();
62+
}
63+
for (let k in interfaces) {
64+
for (let k2 in interfaces[k]) {
65+
let address = interfaces[k][k2];
66+
if (address.family === 'IPv4') {
67+
addresses.push("http://"+address.address+":"+port+"/");
68+
}
69+
}
70+
}
71+
addresses.push("http://localhost:"+port+"/");
72+
res.end('{ "interfaces": ' + JSON.stringify(addresses) + ' }');
73+
});
74+
app.post('/check', (req, res) => {
75+
const reject = () => {
76+
res.setHeader('www-authenticate', 'Basic')
77+
res.sendStatus(401)
78+
}
79+
if (!checkAuth(req.headers.authorization, password)) {
80+
return reject();
81+
}
82+
res.end(mainserver.toString());
83+
});
84+
app.post('/numusers', (req, res) => {
85+
const reject = () => {
86+
res.setHeader('www-authenticate', 'Basic')
87+
res.sendStatus(401)
88+
}
89+
if (!checkAuth(req.headers.authorization, password)) {
90+
return reject();
91+
}
92+
res.end('{ "users": ' + nofusers + " }");
93+
});
5094
server.listen(port || 3000, '0.0.0.0', () => {
5195
consolelog("Starting server on port " + (port || 3000) + " with password " + password);
5296
if(appserver){

src/index.html

Lines changed: 77 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,13 @@
5353
filter: drop-shadow(0 0 10px white);
5454
}
5555

56-
h3 {
57-
font-size: 24px;
58-
margin: 0;
59-
}
60-
6156
.button {
6257
background-color: #0098C5;
6358
color: #000;
6459
font-size: 18px;
6560
border: none;
6661
border-radius: 5px;
6762
padding: 5px 10px;
68-
margin-top: 10px;
6963
cursor: pointer;
7064
}
7165

@@ -79,24 +73,43 @@
7973
}
8074

8175
.url-list {
82-
list-style-type: none;
8376
padding: 0;
77+
display: inline-block;
78+
text-align: left;
79+
margin: 0;
8480
}
8581

8682
.url-list li {
87-
margin-bottom: 5px;
83+
margin-bottom: 2px;
8884
}
8985

90-
.github-link,
91-
.license-link {
86+
a {
9287
color: #0098C5;
9388
text-decoration: none;
9489
}
9590

96-
.github-link:hover,
97-
.license-link:hover {
91+
a:hover {
9892
text-decoration: underline;
9993
}
94+
95+
* {
96+
scrollbar-width: auto;
97+
scrollbar-color: #0097c5 #000000;
98+
}
99+
100+
*::-webkit-scrollbar {
101+
width: 10px;
102+
}
103+
104+
*::-webkit-scrollbar-track {
105+
background: #000000;
106+
}
107+
108+
*::-webkit-scrollbar-thumb {
109+
background-color: #0097c5;
110+
border-radius: 10px;
111+
border: 0.5px solid #006888;
112+
}
100113
</style>
101114
</head>
102115
<body>
@@ -109,19 +122,16 @@
109122
<h1>EmulatorJS Netplay Server</h1>
110123
<img src="img/logo-light.png" alt="Logo" class="logo">
111124
<p id="info">Server info</p>
112-
<h3>Server Options:</h3>
113-
<button onclick="startbutton()" id="server" class="button">Start Server</button>
125+
<button onclick="server()" id="server" class="button">Start Server</button>
114126
<p id="status"></p>
115127
<p id="nuser"></p>
116-
<br>
117-
<p>URL to use with EmulatorJS:</p>
118-
<ul id="urls" class="url-list"></ul>
119-
<br>
128+
<p>URL's to use with EmulatorJS Netplay: <ul id="urls" class="url-list"></ul></p>
120129
<p><a href="https://github.com/EmulatorJS/EmulatorJS-Netplay" target="_blank" class="github-link">View on GitHub</a></p>
121-
<p>Licensed under the Apache License 2.0</p>
122-
<p><a href="https://github.com/EmulatorJS/EmulatorJS-Netplay/blob/main/LICENSE" target="_blank" class="license-link">Read the whole license here</a></p>
130+
<p>Licensed under the Apache License 2.0 - Read the license <a href="https://github.com/EmulatorJS/EmulatorJS-Netplay/blob/main/LICENSE" target="_blank" class="license-link">here</a></p>
131+
<br>
123132
</div>
124133
<script>
134+
const startstop = document.getElementById('server');
125135
window.addEventListener("load", function() {
126136
const loadingScreen = document.getElementById("loading");
127137
const content = document.getElementById("content");
@@ -135,10 +145,18 @@ <h3>Server Options:</h3>
135145
});
136146

137147
(function() {
148+
check();
138149
status().then(info => {
139150
document.getElementById('info').innerHTML = "Running server on port " + (info.port || 3000) + " with password " + info.password;
140151
});
141-
document.getElementById('urls').innerHTML = '<li><a href="'+window.location.protocol+"//"+window.location.hostname+':'+window.location.port+'/" target="_blank" onclick="window.api.openExternal(this.href);event.preventDefault()">'+window.location.protocol+"//"+window.location.hostname+':'+window.location.port+'/</a></li>';
152+
interface().then(address => {
153+
address.interfaces.push(window.location.protocol+"//"+window.location.hostname+':'+window.location.port+'/');
154+
address.interfaces = [...new Set(address.interfaces)];
155+
address.interfaces.forEach(address => {
156+
document.getElementById('urls').innerHTML += '<li><a href="'+address+'" target="_blank" onclick="window.api.openExternal(this.href);event.preventDefault()">'+address+'</a></li>';
157+
});
158+
});
159+
checkforusers();
142160
})();
143161

144162
function status() {
@@ -154,35 +172,24 @@ <h3>Server Options:</h3>
154172
});
155173
}
156174

157-
var startstop = document.getElementById('startStop');
158-
function startbutton(){
159-
if (startstop.textContent === 'Start'){
160-
startstopserver("start");
161-
}else if(startstop.textContent === 'Stop'){
162-
startstopserver("stop");
163-
}
164-
update();
165-
}
166-
function update(){
167-
if(startstop.textContent == "Start"){
168-
document.getElementById('status').style.color = "red";
169-
document.getElementById('status').innerText = 'NOT RUNNING';
170-
}else if(startstop.textContent == "Stop"){
171-
document.getElementById('status').style.color = "green";
172-
document.getElementById('status').innerText = 'RUNNING';
173-
}else{
174-
console.error("Error!");
175-
}
175+
function interface() {
176+
return fetch('/interface', {
177+
method: 'POST',
178+
headers: {
179+
'Content-Type': 'application/json'
180+
}
181+
})
182+
.then(response => response.json())
183+
.then(data => {
184+
return data;
185+
});
176186
}
177187
function check(){
178188
fetch('/check', {
179189
method: 'POST',
180190
headers: {
181191
'Content-Type': 'application/json'
182-
},
183-
body: JSON.stringify({
184-
check: "checking",
185-
})
192+
}
186193
}).then(response => response.json())
187194
.then(data => {
188195
if(data == true){
@@ -193,6 +200,31 @@ <h3>Server Options:</h3>
193200
update();
194201
});
195202
}
203+
function update(){
204+
if(startstop.textContent == "Start"){
205+
document.getElementById('status').style.color = "red";
206+
document.getElementById('status').innerText = 'NOT RUNNING';
207+
}else if(startstop.textContent == "Stop"){
208+
document.getElementById('status').style.color = "green";
209+
document.getElementById('status').innerText = 'RUNNING';
210+
}else{
211+
console.error("Error!");
212+
}
213+
}
214+
function server(){
215+
if (startstop.textContent === 'Start'){
216+
startstopserver("start");
217+
while(startstop.textContent === 'Start'){
218+
check();
219+
}
220+
}else if(startstop.textContent === 'Stop'){
221+
startstopserver("stop");
222+
while(startstop.textContent === 'Stop'){
223+
check();
224+
}
225+
}
226+
update();
227+
}
196228
function startstopserver(option){
197229
fetch('/startstop', {
198230
method: 'POST',
@@ -221,7 +253,6 @@ <h3>Server Options:</h3>
221253
}
222254
update();
223255
});
224-
location.reload();
225256
}
226257
setInterval(function(){
227258
checkforusers();
@@ -232,17 +263,13 @@ <h3>Server Options:</h3>
232263
headers: {
233264
'Content-Type': 'application/json',
234265
'Accept': 'application/json'
235-
},
236-
body: JSON.stringify({
237-
"checkn": true
238-
})
266+
}
239267
}).then(response => response.json())
240268
.then(data => {
241269
data = data.users;
242270
document.getElementById('nuser').innerHTML = "Users connected: "+data;
243271
});
244272
}
245-
checkforusers();
246273
</script>
247274
</body>
248275
</html>

0 commit comments

Comments
 (0)