-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
96 lines (81 loc) · 2.43 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
const express = require('express');
const path = require('path');
const dotenv = require('dotenv');
dotenv.config();
const PORT = process.env.PORT || 5000;
const AccessToken = require('twilio').jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
//init app
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
let users = [];
let rooms = [];
io.on('connection', (socket) => {
users.push(socket.id);
// console.log(`${users.length} users connected`);
socket.on('createChatRoom', ({ room }) => {
// console.log(room);
rooms.push(room);
socket.join(room);
io.sockets.in(room).emit('roomCreated', true);
});
socket.on('joinRoom', ({ room, username, color}) => {
if (rooms.includes(room)) {
socket.join(room);
io.sockets.in(room).emit('message', {
username,
message: `joined the stream`,
color
});
} else {
socket.emit('errorMsg', 'Room doesn\'t exist, invalid URL')
}
});
socket.on('sendChatMessage', ({ room, username, message, color}) => {
// console.log(room, username, message, color);
if (rooms.includes(room)) {
io.sockets.in(room).emit('message', {
username,
message,
color
})
}
});
socket.on('disconnect', () => {
users.splice(socket.id, 1);
// console.log(`user disconnected, ${users.length} left`);
})
});
// static assets
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/remote/:roomId', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'remote.html'));
});
app.get('/generate-token/:roomname/:identity' , (req, res) => {
// Substitute your Twilio AccountSid and ApiKey details
const ACCOUNT_SID = process.env.ACCOUNT_SID;
const API_KEY_SID = process.env.API_KEY_SID;
const API_KEY_SECRET = process.env.API_KEY_SECRET;
// Set the Identity of this token
identity = req.params.identity;
// Grant access to Video
const grant = new VideoGrant({
room: req.params.roomname
});
// Create an Access Token
token = new AccessToken(
ACCOUNT_SID,
API_KEY_SID,
API_KEY_SECRET,
);
token.addGrant(grant);
token.identity = identity;
// Serialize the token as a JWT
const jwt = token.toJwt();
res.status(200).json({ jwt });
});
http.listen(PORT, console.log(`server started on port ${PORT}`));