-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
140 lines (126 loc) · 3.28 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
//Importing Modules
require("dotenv").config();
const express = require("express");
const app = express();
const path = require("path");
const querystring = require('querystring');
const favicon = require('serve-favicon');
const server = require("http").Server(app);
const io = require("socket.io")(server);
const client = require("twilio")(process.env.ACCOUNT_SID, process.env.AUTH_TOKEN);
const { nanoid } = require("nanoid");
const chatUtil = require("./utilities/chatUtil");
//For chat persistence
const chats = {};
//Setting up static serve and request parsing middleware
app.use(express.urlencoded({
extended: true
}));
app.use(express.json());
app.set("view engine", "ejs");
app.use(favicon(path.join(__dirname,'public','images','favicon.ico')));
app.use(express.static(path.join(__dirname,'public')));
//Handle GET "/"
app.get("/", (req, res) => {
res.render("lobby")
});
//Handle GET "/room"
app.get("/room", async (req, res) => {
const roomId = req.query.roomId
const name = req.query.name
try{
if(!name||!roomId){
throw new Error("Illegal Entry");
}
if(chats[roomId]===undefined){
throw new Error("Illegal Entry");
}
token = await client.tokens.create()
const response = token.iceServers;
res.render("room", {
roomId: roomId ,
name: name,
iceServers: response,
chats: chats[roomId],
});
}
catch(err){
if(err.message==="Illegal Entry") res.redirect("/");
else res.send(err);
}
});
//Handle POST "/createRoom"
app.post("/createRoom", (req, res) => {
const roomId = nanoid()
chats[roomId] = [];
res.json({
status : "success",
roomId : roomId
});
});
//Handle POST "/joinRoom"
app.post("/joinRoom",(req,res)=>{
const name = req.body.name
const roomId = req.body.roomId
try{
if(chats[roomId]===undefined){
throw new Error("Room ID invalid");
}
const query = querystring.stringify({
"name": name,
"roomId": roomId
});
const url = "/room/?"+query;
res.json({
status : "success",
body : url
});
}catch(err){
res.json({
status : "error",
body : err.message
});
}
})
//Handle web-socket events
io.on("connection", (socket) => {
socket.on("join-room", (roomId, userId, userName) => {
socket.join(roomId);
socket.to(roomId).emit("user-connected", userId);
const joinAlert = {
sender : "Admin",
body : `${userName} has joined`,
date : chatUtil.getUTCDate(),
control : 1
};
io.to(roomId).emit("create-message", joinAlert);
chats[roomId] = [...chats[roomId],joinAlert];
chatUtil.chatCleanup(chats[roomId]);
socket.on("message", (message) => {
message["date"] = chatUtil.getUTCDate();
message["control"] = 0;
io.to(roomId).emit("create-message", message);
chats[roomId] = [...chats[roomId],message];
chatUtil.chatCleanup(chats[roomId]);
});
socket.on("disconnect", () => {
socket.to(roomId).emit("user-disconnected", userId);
const leaveAlert = {
sender : "Admin",
body : `${userName} has left`,
date : chatUtil.getUTCDate(),
control : 2,
};
io.to(roomId).emit("create-message", leaveAlert);
chats[roomId] = [...chats[roomId],leaveAlert];
chatUtil.chatCleanup(chats[roomId]);
});
});
});
//Handle 404
app.get("*", (req, res) => {
res.redirect("/");
});
server.listen(process.env.PORT || 3000,()=>{
console.log("Server is running")
});