-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
109 lines (100 loc) · 3.42 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
require("dotenv").config
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const PORT = process.env.PORT || 8000;
const path = require('path');
let socketList = {};
app.use(express.static(path.join(__dirname, 'public')));
//for hosting on heroku
if (process.env.PROD) {
app.use(express.static(path.join(__dirname, './client/build')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, './client/build/index.html'));
});
}
// Socket connection
io.on('connection', (socket) => {
console.log(`New User connected: ${socket.id}`);
//On user disconnect
socket.on('disconnect', () => {
socket.disconnect();
console.log('User disconnected!');
});
//check user exists before joining
socket.on('check user exists', ({ roomId, userName }) => {
let error = false;
io.sockets.in(roomId).clients((err, clients) => {
clients.forEach((client) => {
if (socketList[client] == userName) {
error = true;
}
});
socket.emit('error user exists', { error });
});
});
/**
* Join Room
*/
socket.on('join room', ({ roomId, userName }) => {
// Socket Join RoomName
socket.join(roomId);
socketList[socket.id] = { userName, video: true, audio: true };
// Set User List
io.sockets.in(roomId).clients((err, clients) => {
try {
const users = [];
clients.forEach((client) => {
// Add User List
users.push({ userId: client, info: socketList[client] });
});
socket.broadcast.to(roomId).emit('all users', users);
} catch (e) {
io.sockets.in(roomId).emit('error user exists', { err: true });
}
});
});
//add user to the room
socket.on('add user', ({ userToCall, from, signal }) => {
io.to(userToCall).emit('user joined', {
signal,
from,
info: socketList[socket.id],
});
});
//server receives signal from user and send to the room
socket.on('sending signal', ({ signal, to }) => {
io.to(to).emit('accepting signal', {
signal,
answerId: socket.id,
});
});
//chat messages are passed
socket.on('send chat message', ({ roomId, msg, sender }) => {
io.sockets.in(roomId).emit('receive chat message', { msg, sender });
});
//user leave event
socket.on('remove user', ({ roomId, user }) => {
let userInfo = socketList[socket.id];
delete socketList[socket.id];
socket.broadcast
.to(roomId)
.emit('user left', { userId: socket.id, info: userInfo });
io.sockets.sockets[socket.id].leave(roomId);
});
//toggling user video and audio
socket.on('change video audio', ({ roomId, switchTarget }) => {
if (switchTarget === 'video') {
socketList[socket.id].video = !socketList[socket.id].video;
} else {
socketList[socket.id].audio = !socketList[socket.id].audio;
}
socket.broadcast
.to(roomId)
.emit('change vid aud', { userId: socket.id, switchTarget });
});
});
http.listen(PORT, () => {
console.log(`Server is running at Port ${PORT}`);
});