-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·117 lines (105 loc) · 3.18 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
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
106
107
108
109
110
111
112
113
114
115
const express = require('express');
const history = require('connect-history-api-fallback');
const app = express();
const port = 5555;
const server = app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
});
const io = require("socket.io")(server, {
cors: {
origin: '*'
}
})
const staticFileMiddleware = express.static(__dirname + '/public');
app.use(staticFileMiddleware);
app.use(history({
disableDotRule: true,
verbose: true
}));
app.use(staticFileMiddleware);
var rooms = new Map()
var Room = function (meetID) {
this.meetID = meetID;
this.emtpy = true;
this.addMember = (member) => {
if (this.full) return false
this.members.push(member)
this.emtpy = false
if (this.members.length == 2) {
this.full = true
}
return true
}
this.deleteMember = (member) => {
if (this.emtpy) return false
var mi = this.members.findIndex(member)
this.members.splice(mi, 1)
this.full = false
if (this.members.length == 0) {
this.emtpy = true
}
return true
}
this.getOtherMember = (member) => {
if (this.emtpy) return false
var m = this.members[0]
if (m == member) {
return this.members[1]
}
else return m
}
this.getMembers = () => {
return this.members
}
this.full = false;
this.members = []
return this
}
var roomExist = (meetID) => rooms.has(meetID)
var createRoom = (meetID) => {
var r = new Room(meetID)
rooms.set(meetID, r)
return r
}
var getRoom = (meetID) => rooms.get(meetID)
io.on('connection', (socket) => {
console.log("User connected : "+ socket.id)
socket.on('join', (meetData) => {
if (roomExist(meetData.meetID)) {
var room = getRoom(meetData.meetID)
if (room.full) socket.emit('roomFull')
else {
if (room.addMember(socket)) {
socket.emit('joined')
}
}
} else {
var room = createRoom(meetData.meetID)
if (room.full) socket.emit('roomFull')
else {
if (room.addMember(socket)) socket.emit('created')
}
}
})
socket.on('iceCandidate', (meetData) => {
var room = getRoom(meetData.meetID)
var otherMember = room.getOtherMember(socket)
otherMember.emit('iceCandidate', { candidate: meetData.candidate })
// console.log("Candidate send from : " + socket.id)
})
socket.on('offer', (meetData) => {
var room = getRoom(meetData.meetID)
var otherMember = room.getOtherMember(socket)
otherMember.emit('offer', { sdp: meetData.sdp, type: meetData.type })
})
socket.on('answer', (meetData) => {
var room = getRoom(meetData.meetID)
var otherMember = room.getOtherMember(socket)
otherMember.emit('answer', { sdp: meetData.sdp, type: meetData.type })
})
socket.on('initiateWebRTC', (meetData) => {
var room = getRoom(meetData.meetID)
var otherMember = room.getOtherMember(socket)
otherMember.emit('initiateWebRTC')
})
})