-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
184 lines (140 loc) · 5.33 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const path = require('path');
const express = require('express')
const http = require('http')
const moment = require('moment');
// @ts-ignore
const socketio = require('socket.io');
const PORT = process.env.PORT ||8000;
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// const { auth } = require('express-openid-connect');
// const config = {
// authRequired: false,
// auth0Logout: true,
// secret: 'a long, randomly-generated string stored in env',
// baseURL: 'http://localhost:5500',
// clientID: '1GbGXUZLO27KnRvazXRQge2XSiKR2T1O',
// issuerBaseURL: 'https://bearahand.us.auth0.com'
// };
// // auth router attaches /login, /logout, and /callback routes to the baseURL
// app.use(auth(config));
// // req.isAuthenticated is provided from the auth router
// app.get('/', (req, res) => {
// res.send(req.oidc.isAuthenticated() ? 'Logged in' : 'Logged out');
// });
// authentication server js
// require('dotenv').config();
// const { auth } = require('express-openid-connect');
// app.use(
// auth({
// issuerBaseURL: process.env.ISSUER_BASE_URL,
// baseURL: process.env.BASE_URL,
// clientID: process.env.CLIENT_ID,
// secret: process.env.SECRET,
// idpLogout: true,
// })
// );
app.use(express.static(path.join(__dirname, 'public')));
// // req.isAuthenticated is provided from the auth router
// app.get('/', (req, res) => {
// res.send(req.oidc.isAuthenticated() ? 'Logged in' : 'Logged out');
// });
// app.get('/profile', requiresAuth(), (req, res) => {
// res.send(JSON.stringify(req.oidc.user));
// });
let rooms = {};
let socketroom = {};
let socketname = {};
let micSocket = {};
let videoSocket = {};
let roomBoard = {};
// connection establishing and other calls
io.on('connect', socket => {
socket.on("join room", (roomid, username) => {
socket.join(roomid);
socketroom[socket.id] = roomid;
socketname[socket.id] = username;
micSocket[socket.id] = 'on';
videoSocket[socket.id] = 'on';
if (rooms[roomid] && rooms[roomid].length > 0) {
rooms[roomid].pushs(socket.id);
socket.to(roomid).emit('message', `${username} joined the room.`, 'Teams:', moment().format(
"h:mm a"
));
io.to(socket.id).emit('join room', rooms[roomid].filter(pid => pid != socket.id), socketname, micSocket, videoSocket);
}
else {
rooms[roomid] = [socket.id];
io.to(socket.id).emit('join room', null, null, null, null);
}
io.to(roomid).emit('user count', rooms[roomid].length);
});
socket.on('action', msg => {
if (msg == 'mute')
micSocket[socket.id] = 'off';
else if (msg == 'unmute')
micSocket[socket.id] = 'on';
else if (msg == 'videoon')
videoSocket[socket.id] = 'on';
else if (msg == 'videooff')
videoSocket[socket.id] = 'off';
socket.to(socketroom[socket.id]).emit('action', msg, socket.id);
})
socket.on('video-offer', (offer, sid) => {
socket.to(sid).emit('video-offer', offer, socket.id, socketname[socket.id], micSocket[socket.id], videoSocket[socket.id]);
})
socket.on('video-answer', (answer, sid) => {
socket.to(sid).emit('video-answer', answer, socket.id);
})
socket.on('new icecandidate', (candidate, sid) => {
socket.to(sid).emit('new icecandidate', candidate, socket.id);
})
socket.on('message', (msg, username, roomid) => {
io.to(roomid).emit('message', msg, username, moment().format(
"h:mm a"
));
})
socket.on('getCanvas', () => {
if (roomBoard[socketroom[socket.id]])
socket.emit('getCanvas', roomBoard[socketroom[socket.id]]);
});
socket.on('draw', (newx, newy, prevx, prevy, color, size) => {
socket.to(socketroom[socket.id]).emit('draw', newx, newy, prevx, prevy, color, size);
})
socket.on('clrbrd', () => {
socket.to(socketroom[socket.id]).emit('clrbrd');
});
socket.on('store canvas', url => {
roomBoard[socketroom[socket.id]] = url;
})
// disconnecting event and id delete
socket.on('disconnect', () => {
if (!socketroom[socket.id]) return;
socket.to(socketroom[socket.id]).emit('message', `${socketname[socket.id]} left the chat.`, `Teams`, moment().format(
"h:mm a"
));
socket.to(socketroom[socket.id]).emit('remove peer', socket.id);
var index = rooms[socketroom[socket.id]].indexOf(socket.id);
rooms[socketroom[socket.id]].splice(index, 1);
io.to(socketroom[socket.id]).emit('user count', rooms[socketroom[socket.id]].length);
delete socketroom[socket.id];
logger.info('--------------------');
logger.info(rooms[socketroom[socket.id]]);
//toDo: push socket.id out of rooms
});
})
// basic debugging logger added
const pino = require('pino');
const expressPino = require('express-pino-logger');
const logger = pino({ level: process.env.LOG_LEVEL || 'info' });
const expressLogger = expressPino({ logger });
app.use(expressLogger);
app.get('/', (req, res) => {
logger.debug('Calling res.send');
res.send('Hello World');
});
server.listen(PORT, () => {
// logger.info('Application is running');
logger.info(`Server is up and running on port ${PORT}`);
});