-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (50 loc) · 1.55 KB
/
Copy pathserver.js
File metadata and controls
57 lines (50 loc) · 1.55 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
const path = require('path');
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
app.use(express.static(__dirname + "/public"));
app.use(express.static(__dirname + "/views"));
app.set('view engine', 'pug');
var clients = 0;
io.on('connection', function (socket) {
socket.on("NewClient", function () {
// Communication between only 2 clients
if (clients < 2) {
if (clients == 1) {
// If the first client make it the
// initator of the peer
this.emit('initiatorClient');
}
}
else
this.emit('sessionActive');
clients++;
console.log("Client Added");
})
// Sending the request to other clients
socket.on('Request', (data) => {
socket.broadcast.emit("ServerRequest", data);
})
// Sending the respond to other clients
socket.on('Respond', (data) => {
socket.broadcast.emit("ServerRespond", data);
})
// If connection closes
socket.on('disconnect', Disconnect);
})
function Disconnect() {
if (clients > 0) {
if (clients <= 2)
this.broadcast.emit("Disconnect");
clients--;
}
}
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname+'/views/login.html'));
});
app.get('/video', function (req, res) {
res.sendFile(path.join(__dirname+'/views/video.html'));
});
http.listen(port, () => console.log('Running on port '));