-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (72 loc) · 2.08 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
require('dotenv').config()
const express = require('express')
const { ExpressPeerServer } = require('peer')
const port = process.env.PORT || 3000
const host = process.env.BASE_URL || 'localhost'
const peerConfig = host === 'localhost' ? {
path: '/peerjs/server',
port: port.toString(),
host: host
} : {
path: '/peerjs/server',
host: host
}
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)
const { v4: uuidV4 } = require('uuid')
const { pressClips, photos } = require('./data/media')
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.get('/', (req, res) => {
res.render('home')
})
app.get('/about', (req, res) => {
res.render('about', { pressClips: pressClips })
})
app.get('/shows', (req, res) => {
res.render('shows', { photos: photos })
})
app.get('/contact', (req, res) => {
res.render('contact')
})
app.get('/press', (req, res) => {
res.render('press')
})
app.get('/team', (req, res) => {
res.render('team')
})
app.get('/greta', (req, res) => {
res.render('greta', { peerConfig: peerConfig })
})
app.get('/hi', (req, res) => {
res.redirect(`/hi/${uuidV4()}`)
})
app.get('/hi/:room', (req, res) => {
res.render('room', { roomId: req.params.room, peerConfig: peerConfig })
})
let messages = [];
io.on('connection', socket => {
socket.on('join-room', (roomId, userId) => {
console.log('someone joined', roomId, userId)
socket.join(roomId)
socket.to(roomId).broadcast.emit('user-connected', userId)
io.emit("chat message", messages);
socket.on('disconnect', () => {
socket.to(roomId).broadcast.emit('user-disconnected', userId)
})
socket.on("chat message", function (data) {
console.log(data)
messages.push(data)
if (messages.length > 100) {
messages.shift()
}
io.emit("chat message", messages)
});
})
})
server.listen(port)
const peerServer = ExpressPeerServer(server, {
path: '/server'
})
app.use('/peerjs', peerServer)