-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (131 loc) · 4.34 KB
/
index.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
/*
* Created by Lucas Chapman
*
* This project was based on https://p2p-video-chat-application.freecodecamp.rocks/
* for the purposes of creating a project for freeCodeCamp
*/
require("dotenv").config();
const express = require("express");
/**
* Module that contains the entire application
* @module ./index
*
*/
const app = express();
// Socket.io Setup
const { createServer } = require("http");
const { Server } = require("socket.io");
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
},
});
// Helmet Setup
const helmet = require("helmet");
app.use(helmet.xssFilter());
// Allows stylesheets, JS scripts, and other files to be loaded
app.use(express.static(__dirname + "/public"));
// Allows EJS to be used instead of HTML
app.set("view engine", "ejs");
// Displays index.ejs by default
app.get("/", (req, res) => {
res.render("index", { room: "" });
});
// Display index.ejs with the room that was entered
app.get("/:room", (req, res) => {
res.render("index", { room: req.params.room });
});
// Socket.io Handling
let connectedUsers = []; // Array of users connected to a room
let userMessages = []; // Array of messages
// Creates listeners for all sockets
io.on("connection", (socket) => {
// Removes the user that left from connectedUsers
socket.on("disconnect", () => {
connectedUsers = connectedUsers.filter((user) => user.id !== socket.id);
socket.broadcast.emit("updateUserList", { users: connectedUsers });
console.log("A user disconnected");
});
// Connects the user to the room and creates more listeners
socket.on("joinRoom", (room, username) => {
socket.join(room);
// Clear previous messages when no one is in the room
if (
connectedUsers.filter((user) => user.room === room).length == 0 ||
connectedUsers.length == 0
) {
userMessages = userMessages.filter((obj) => obj.room !== room);
}
// Checks if a new user has been connected
let result =
connectedUsers.length > 0
? connectedUsers.find((user) => user.id === socket.id)
: undefined;
// Adds the new user to the array
if (result == undefined) {
connectedUsers.push({
id: socket.id,
username: username,
room: room,
});
}
console.log("A user join room '" + room + "'");
// Sends the updated list of users to the client
socket.on("requestUserList", () => {
io.to(room).emit("updateUserList", { users: connectedUsers });
});
// Removes the user from a full room and allows the client to display a message
socket.on("removeUser", (user) => {
if (
socket.id === user.id &&
connectedUsers.filter((u) => u.room === user.room).length > 2
) {
console.log("user was removed");
socket.leave(user.room);
connectedUsers = connectedUsers.filter((u) => u.id != user.id);
}
// Sends the message ONLY to the user that was removed
socket.to(user.id).emit("roomFull");
});
// Tells the client to create a media offer for the second peer
socket.on("mediaOffer", (data) => {
socket.to(data.to).emit("mediaOffer", {
from: data.from,
offer: data.offer,
});
});
// Tells the client to create a media answer for the first peer
socket.on("mediaAnswer", (data) => {
socket.to(data.to).emit("mediaAnswer", {
from: data.from,
answer: data.answer,
});
});
// Tells the client to add the ICE candidate for all peers
socket.on("iceCandidate", (data) => {
socket.to(data.to).emit("remotePeerIceCandidate", {
candidate: data.candidate,
});
});
// Sends all messages in the room to the client
socket.on("requestMessages", () => {
socket.emit("getMessages", { userMessages: userMessages });
});
// Adds messages to userMessages and adds a message to the room
socket.on("message", (message) => {
userMessages.push({
room: room,
message: message,
userId: socket.id,
username: username,
});
io.to(room).emit("createMessage", message, socket.id, username);
});
});
});
// Sets the port used to access my app
const listener = server.listen(process.env.PORT || 8080, () => {
console.log("Your app is listening on port " + listener.address().port);
});
module.exports = app; // For unit testing