From ce4845d0e8c4ddb87c5568cff47623ad8bd575b7 Mon Sep 17 00:00:00 2001 From: Simon Giesel Date: Fri, 27 Apr 2018 14:38:19 +0200 Subject: [PATCH] Get host dynamicly --- server.js | 30 +++++++++++++++++++++++++++++- src/views/server.hbs | 10 +++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index b9f9441..9a19f2b 100644 --- a/server.js +++ b/server.js @@ -11,8 +11,34 @@ const express = require('express'), const scores = JSON.parse(fs.readFileSync('scores.json', 'utf8')); +// Detect local IP +let os = require('os'); +let ifaces = os.networkInterfaces(); +let ip; + +Object.keys(ifaces).forEach(function (ifname) { + let alias = 0; + + ifaces[ifname].forEach(function (iface) { + if ('IPv4' !== iface.family || iface.internal !== false) { + // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses + return; + } + + if (alias >= 1) { + // this single interface has multiple ipv4 addresses + console.log(ifname + ':' + alias, iface.address); + } else { + // this interface has only one ipv4 adress + console.log(ifname, iface.address); + ip = iface.address; + } + ++alias; + }); +}); + /** CONSTANTS */ -const HOST = 'http://100.119.37.224:8080/client/'; +const HOST = 'http://' + ip + ':8080/client/'; const players = [ { @@ -145,6 +171,8 @@ monitorNSP.on('connection', socket => { socket.emit('handshake', { scores, + marioURL: HOST + players[0].id, + luigiURL: HOST + players[1].id, marioId: players[0].id, luigiId: players[1].id, }); diff --git a/src/views/server.hbs b/src/views/server.hbs index 9385a28..e1fa8d4 100644 --- a/src/views/server.hbs +++ b/src/views/server.hbs @@ -96,7 +96,12 @@

Super - MARIO

+ M + A + R + I + O +

QR-Code scannen um zu spielen

@@ -199,7 +204,10 @@ document.querySelector('ol[start="1"]').innerHTML = ''; document.querySelector('mario a img').src = 'assets/mario.qr.png?' + options.marioId; document.querySelector('luigi a img').src = 'assets/luigi.qr.png?' + options.luigiId; + document.querySelector('mario a').href = options.marioURL; + document.querySelector('luigi a').href = options.luigiURL; scores = options.scores.sort(sortHighToLow).reverse(); + offset = 0; console.log(scores); for (let i in scores) { if (!scores[i].name)