-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (86 loc) · 2.79 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
const path = require('path')
const fs = require('fs')
const express = require('express')
const app = express()
const server = require('http').Server(app)
const port = process.env.PORT || 3000
const bodyParser = require('body-parser')
const io = require('socket.io')(server)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// Set public folder as root
app.use('/', express.static(__dirname + '/public'))
// Provide access to node_modules folder
app.use('/scripts', express.static(__dirname + '/node_modules/'))
// Redirect all traffic to index.ejs
app.get('/', (req, res) => {
res.render('stream')
})
// Pass params to index.ejs
app.get('/watch/:token', (req, res) => {
res.render('watch', {
token: req.params.token
})
})
var nsp = []
app.get('/room/:hash', (req, res) => {
let broadcaster
const data = req.params.hash
if (nsp[data] === undefined) {
nsp[data] = io.of('/' + data)
console.log(`Created namespace ${data}`)
nsp[data].on("error", e => console.log(e))
nsp[data].on("connection", socket => {
// webrtc connection
socket.on("broadcaster", () => {
broadcaster = socket.id
socket.broadcast.emit("broadcaster")
})
socket.on("watcher", () => {
socket.to(broadcaster).emit("watcher", socket.id)
})
socket.on("offer", (id, message) => {
socket.to(id).emit("offer", socket.id, message)
})
socket.on("answer", (id, message) => {
socket.to(id).emit("answer", socket.id, message)
})
socket.on("candidate", (id, message) => {
socket.to(id).emit("candidate", socket.id, message)
})
socket.on("disconnect", () => {
if(broadcaster == socket.id)
socket.broadcast.emit('disconnectBroadcaster')
socket.to(broadcaster).emit("disconnectPeer", socket.id)
})
// save video record
socket.on('saveVideoStream', (filename, buffer) => {
if(writeToDisk(filename, buffer) == true)
socket.broadcast.emit('savedVideoStream')
})
})
} else {
console.log(`Namespace ${data} already exists`)
}
//rend status 200
res.sendStatus(200)
})
server.listen(port, () => {
// eslint-disable-next-line no-console
console.info('listening on %d', server.address().port)
})
function writeToDisk(filename, buffer) {
//path to store uploaded files (NOTE: presumed you have created the folders)
const uploadedFile = __dirname + '/uploads/' + filename + '.webm'
fs.open(uploadedFile, 'a', 0755, (err, fd) => {
if (err) throw err
fs.write(fd, buffer, null, 'Binary', (err, written, buff) => {
fs.close(fd, () => {
console.log('File saved successfully!')
})
})
})
return true
}