-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (37 loc) · 921 Bytes
/
server.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
40
41
42
43
44
45
46
47
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'))
let clients = 0
io.on('connection', function (socket) {
socket.on('NewClient', function () {
if (clients < 2) {
if (clients == 1) {
this.emit('CreatePeer')
}
} else {
this.emit('SessionActive')
}
clients++;
})
socket.on('Offer', SendOffer)
socket.on('Answer', SendAnswer)
socket.on('disconnect', Disconnect)
})
function Disconnect() {
if (clients > 0) {
if (clients <= 2) {
this.broadcast.emit("Disconnect")
}
clients--;
}
}
function SendOffer(offer) {
this.broadcast.emit('BackOffer', offer)
}
function SendAnswer(data) {
this.broadcast.emit('BackAnswer', data)
}
http.listen(port, () => console.log("Active"))