-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
105 lines (83 loc) · 2.63 KB
/
server.js
File metadata and controls
105 lines (83 loc) · 2.63 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//npm run build on reacttest folder
//process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const express = require('express');
const app = express();
const fs = require('fs');
//var https = require('https');
var http = require('http');
//const server = require('http').createServer(app);
/*
var options = {
key: fs.readFileSync('./invalidCerts/96461604_192.168.1.43.key'),
cert: fs.readFileSync('./invalidCerts/96461604_192.168.1.43.cert'),
rejectUnauthorized: false,
requestCert: false
}
*/
var server = http.createServer(app);
//var serverUnsecure = http.createServer(app)
const io = require('socket.io')(server);
const path = require('path');
var port = process.env.PORT || 5000;
var queue = [];
app.use(function (req, res, next) {
if(req.secure || req.header('x-forwarded-proto') == 'https') {
next()
} else {
res.redirect("https://" + req.headers.host + req.url );
}
})
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
/*
function redirectSec(req, res, next) {
if (!req.secure) {
res.redirect('https://' + req.headers.host + req.path);
} else {
return next();
}
}
*/
server.listen(port);
//serverUnsecure.listen(80);
io.on('connection', (socket) => {
console.log("User Connected");
//socket.interest = socket.interest.toLowerCase();
/*
if(typeof queue[socket.interest] != 'undefined') {
if(queue[socket.interest]) {
//Client's interest exist
if(queue[socket.interest].length > 0) {
//Another client with similar interest is already in the queue, needs to be matched
} else {
//Client is added to queue, must be the simple-peer initiator
if(!socket.currentlySearching) {
queue[socket.interest].push(socket);
socket.emit('becomeInitiator');
socket.currentlySearching = true;
}
}
}
}
*/
if(queue.length <= 0) {
socket.emit('peer', {initiator: true});
console.log("Client told to become initiator");
console.log("Users in queue: " + queue.length);
} else {
socket.emit('peer', {initiator: false});
socket.emit('joinInitiator', queue.pop());
console.log("Users in queue: " + queue.length);
}
socket.on('initiatorData', function(data) {
var socketid = socket.id;
queue.push({socketid: socketid, data});
});
socket.on('backToInitiator', function(data) {
var socketData = data;
io.to(socketData.socketid).emit("toInitiatorFromServer", data);
})
});
console.log("Server is listening on port: " + port);