forked from jon-Silveira/Webrtc_Audio_Mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
130 lines (92 loc) · 4.21 KB
/
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
console.log('calling form parent process');
// This Code is modified from https://github.com/borjanebbal/webrtc-node-app
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
app.use('/', express.static('public'))
io.on('connection', (socket) => {
console.log("connected")
socket.on('join', (roomId) => {
let numberOfClients = 0;
if (io.sockets.adapter.rooms.has(roomId)) numberOfClients = io.sockets.adapter.rooms.get(roomId).size
// These events are emitted only to the sender socket.
if (numberOfClients == 0) {
console.log(`Creating room ${roomId} and emitting room_created socket event`)
//create a room with the room ID
socket.join(roomId)
socket.emit('room_created', roomId)
}
// else if (numberOfClients == 1) {
// // if there are some one in the room
// // emit your id to the person in the room
// console.log(`Joining room ${roomId} and emitting room_joined socket event`)
// socket.join(roomId)
// const clientd = io.sockets.adapter.rooms.get(roomId)
// let clientlist = []
// for (const clientId of clientd ) {clientlist.push(clientId)}
// console.log(clientlist)
// // socket.emit('room_joined', roomId)
// socket.emit('room_joined2',socket.id, numberOfClients, clientlist, roomId)
// // console.log(socket.id, numberOfClients, clientd, roomId)
// }
else if (numberOfClients >= 1) {
// if there are some one in the room
// emit your id to the person in the room
console.log(`Joining room ${roomId} and emitting room_joined socket event`)
socket.join(roomId)
//socket.emit('room_joined', roomId)
const clientd = io.sockets.adapter.rooms.get(roomId)
let clientlist = []
for (const clientId of clientd ) {clientlist.push(clientId)}
console.log(clientlist)
socket.emit('room_joined2', socket.id, numberOfClients, clientlist, roomId)
}
else {
// This limits the amount of people in the room
// potentially for optimization reasons
console.log(`Can't join room ${roomId}, emitting full_room socket event`)
socket.emit('full_room', roomId)
}
})
// These events are emitted to all the sockets connected to the same room except the sender.
// when a user enters a room it emits a call
socket.on('start_call2', (roomId) => {
console.log(`Broadcasting start_call event to peers in room ${roomId}`)
const clientd = io.sockets.adapter.rooms.get(roomId)
let clientlist = []
for (const clientId of clientd ) {clientlist.push(clientId)}
socket.to(roomId).emit('start_call2',socket.id, clientlist.length, clientlist, roomId)
})
//
socket.on('webrtc_offer2', (event, toID) => {
console.log(`Broadcasting webrtc_offer event to peers in room ${event.roomId}`)
const clientd = io.sockets.adapter.rooms.get(event.roomId)
let clientlist = []
for (const clientId of clientd ) {clientlist.push(clientId)}
io.to(toID).emit('webrtc_offer2',socket.id, clientlist.length, clientlist, event.sdp)
//socket.to(event.roomId).emit('webrtc_offer2', event.sdp)
})
socket.on('webrtc_answer2', (event, toID) => {
console.log(`Broadcasting webrtc_answer event to peers in room ${event.roomId}`)
//socket.to(event.roomId).emit('webrtc_answer2', event.sdp,socket.id )
io.to(toID).emit('webrtc_answer2', event.sdp,socket.id )
})
socket.on('webrtc_ice_candidate', (event, toID) => {
console.log(`Broadcasting webrtc_ice_candidate event to peers in room ${event.roomId}`)
io.to(toID).emit('webrtc_ice_candidate', event, socket.id)
//socket.to(event.roomId).emit('webrtc_ice_candidate', event, socket.id)
})
socket.on('disconnect', function() {
io.sockets.emit("user-left", socket.id);
})
socket.on('dis_con', function(event) {
socket.leave(event.roomId)
io.sockets.emit("user-left", socket.id);
})
})
// START THE SERVER =================================================================
const port = process.env.PORT || 3123
server.listen(port, () => {
console.log(`NEW Express server listening on port ${port}`)
})