-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (56 loc) · 2.53 KB
/
index.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
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
app.use(express.static('lib'))
app.use(express.static('node_modules/ace-builds/src-noconflict'))
app.use('/', express.static('public'))
io.on('connection', (socket) => {
socket.on('join', (roomId, user_name) => {
const roomClients = io.sockets.adapter.rooms[roomId] || { length: 0 }
const numberOfClients = roomClients.length
// These events are emitted only to the sender socket.
if (numberOfClients == 0) {
console.log(`Creating room ${roomId} and emitting room_created socket event`)
socket.join(roomId)
socket.emit('room_created', roomId)
socket.emit('init', { id: roomId })
// socket.on('message', op => { socket.broadcast.emit('message', op) })
} else {
console.log(`Joining room ${roomId} and emitting room_joined socket event`)
socket.join(roomId)
socket.emit('room_joined', roomId)
socket.emit('init', { id: roomId })
// socket.on('message', op => { socket.broadcast.emit('message', op) })
}
})
// These events are emitted to all the sockets connected to the same room except the sender.
socket.on('start_call', (roomId) => {
console.log(`Broadcasting start_call event to peers in room ${roomId}`)
socket.broadcast.to(roomId).emit('start_call')
})
socket.on('webrtc_offer', (event) => {
console.log(`Broadcasting webrtc_offer event to peers in room ${event.roomId}`)
socket.broadcast.to(event.roomId).emit('webrtc_offer', event.sdp)
})
socket.on('webrtc_answer', (event) => {
console.log(`Broadcasting webrtc_answer event to peers in room ${event.roomId}`)
socket.broadcast.to(event.roomId).emit('webrtc_answer', event.sdp)
})
socket.on('webrtc_ice_candidate', (event) => {
console.log(`Broadcasting webrtc_ice_candidate event to peers in room ${event.roomId}`)
socket.broadcast.to(event.roomId).emit('webrtc_ice_candidate', event)
})
socket.on('message', op => {console.log(op.id, op); socket.broadcast.to(op.id).emit('message', op) })
socket.on('download-file', (event) => {
socket.broadcast.to(event.roomId).emit('download-file', event.file_desc)
})
socket.on('text-message', (event) => {
socket.broadcast.to(event.roomId).emit('text-message', event)
})
})
// START THE SERVER =================================================================
const port = process.env.PORT || 3000
server.listen(port, () => {
console.log(`Express server listening on port ${port}`)
})