forked from Larrystamford/web-rtc-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignalling_server.js
More file actions
61 lines (45 loc) · 1.87 KB
/
signalling_server.js
File metadata and controls
61 lines (45 loc) · 1.87 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
58
const { v4: uuidV4 } = require('uuid')
const express = require('express')
const app = express()
const fs = require('fs');
const https = require('https')
const signalling_server = https.createServer({
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem'),
}, app)
const io = require('socket.io')(signalling_server)
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.get('/', (req, res) => {
res.redirect(`/${uuidV4()}`)
})
app.get('/:room', (req, res) => {
res.render('room', { roomId: req.params.room })
})
io.on('connection', socket => {
socket.on('join-room', (roomId, userId) => {
socket.join(roomId)
// broadcast.emit vs .emit
// if broadcast, message is not sent to self
console.log("INFO: " + userId + " has joined room " + roomId);
socket.to(roomId).broadcast.emit('another-user-entered', userId)
socket.on('offer', (localDescription, userId, targetId) => {
console.log(`INFO: ${userId} has presented WebRTC Offer to ${targetId}:` + localDescription);
socket.to(roomId).broadcast.emit('offer', localDescription, userId, targetId)
})
socket.on('answer', (localDescription, userId, targetId) => {
console.log(`INFO: ${userId} has replied ${targetId} with WebRTC Answer:` + localDescription);
socket.to(roomId).broadcast.emit('answer', localDescription, userId, targetId)
})
socket.on('new-ice-candidate', (candidate, userId, targetId) => {
console.log(`INFO: ${userId} has new ICE candidate:` + candidate);
socket.to(roomId).broadcast.emit('new-ice-candidate', candidate, userId, targetId)
})
// when client closes browser, it automatically emits a disconnect
socket.on('disconnect', () => {
console.log("INFO: " + userId + " has disconnected");
socket.to(roomId).broadcast.emit('user-disconnected', userId)
})
})
})
signalling_server.listen(3000)