-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
112 lines (88 loc) · 2.95 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
const express = require('express');
const app = express();
const server = require('http').Server(app);
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const io = require('socket.io')(server);
const dotenv = require('dotenv');
const PORT = process.env.PORT || 443;
// Peer
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
debug: true,
});
//configuring environment variables
dotenv.config();
// server side realtime database
const admin = require('firebase-admin');
var serviceAccount = require(process.env.MY_CREDENTIAL);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.databaseURL
});
// app started
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use('/peerjs', peerServer);
app.get( '/', (req, res) => {
res.render('homepage');
});
app.get( '/teams-webrtc', (req, res) => {
res.redirect(`/${uuidv4()}`);
});
app.get( '/main+:room', (req, res) => {
res.render('main',{ roomId: req.params.room });
});
app.get( '/:room', (req, res) => {
res.render('room', { roomId: req.params.room });
});
io.on('connection', (socket) => {
socket.on('join-room', (roomId, userId ,userName,state) => {
socket.join(roomId);
socket.broadcast.to(roomId).emit('user-connected', userId , state);
var userRooms= admin.database().ref(`users/${userName}/rooms/${roomId}`);
if((roomId+"").length===36)
userRooms.push().set({
"name": roomId
});
socket.on('message', (message) => {
io.to(roomId).emit('createMessage', message , userName, userId);
});
if(state ==="in-meet")
{
// firebase code
var meetParticipantsRef =admin.database().ref(roomId).child("meetParticipants").child(userName);
var arrival_time = (new Date()).getTime();
var currentParticipants= admin.database().ref(`${roomId}/currentParticipants/${userName}`);
currentParticipants.push().set({
"name": userName
});
//firebase code
socket.on("disconnect", (reason)=>{
meetParticipantsRef.push().set({
"tempo_id": userId,
"arrival_time": arrival_time,
"disconnected_time": (new Date()).getTime()
});
socket.broadcast.emit("user-disconnected", userId , userName );
admin.database().ref(`${roomId}/currentParticipants/${userName}`).remove();
});
socket.on('waved', (myName) => {
io.to(roomId).emit('toggleWave', myName);
});
socket.on('change-filter', (videoId,filter) => {
io.to(roomId).emit('change-filter', videoId,filter);
});
/**
* Whiteboard actions for all user in the same room
*/
socket.on("wb", (config) => {
io.to(roomId).emit("wb", config);
});
socket.on('screen-closed', (screenId) => {
io.to(roomId).emit('remove-screen', screenId);
});
}
});
});
server.listen( PORT , () => console.log(`Server listening to port ${PORT}`));