-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
39 lines (34 loc) · 1.14 KB
/
app.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
const express = require('express');
const swarm = require('discovery-swarm')
const webSocket = require(__dirname + '/websockets');
const ip = require('ip');
const app = express();
const serverPort = 5000 + Math.floor((Math.random() * 500) + 1);
const swPort = 6000 + Math.floor((Math.random() * 500) + 1);
const sw = swarm({maxConnections: 1});
sw.listen(swPort);
sw.join('PingPong');
// For static front-end files
app.use(express.static('views'));
app.set('view engine', 'ejs');
const server = app.listen(serverPort, () => {console.log("Game available on: " + ip.address() + ":" + serverPort) });
// Main Endpoint
app.get('/', (req, res) => {res.render('index', {"ip" : ip.address(), "port" : serverPort})});
// Discovered Peer
var timer = null;
var connected = false;
sw.on('connection', function(conn, info) {
if(connected){return;}
if(!conn.server){
timer = setTimeout(() => {
connected = true;
webSocket.socketConnection(server, {"conn" : conn, "side" : "right"})
}, 1000)
}
else{
connected = true;
// Socket Api
webSocket.socketConnection(server, {"conn" : conn, "side" : "left"});
clearTimeout(timer)
}
})