-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
70 lines (54 loc) · 2.2 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
const express = require("express");
const path = require("path");
const http = require("http");
const socket = require("socket.io");
const pagesRoutes = require("./routes/pages");
const databaseRoutes = require("./routes/db");
const formatMessage = require("./utils/messages");
const { userJoin, getCurrentUser, userLeave, saveMessage, saveChat } = require("./utils/room-users");
const app = express();
const server = http.createServer(app);
const io = socket(server);
const PORT = process.env.PORT || 3000;
const publicDirectory = path.join(__dirname, "./public");
const chatBot = "Seguro Bot";
app.use(express.static(publicDirectory));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
//Setting view engine to hbs to handle hbs files
app.set("view engine", "hbs");
//Setting routes
app.use("/", pagesRoutes); // GET requests
app.use("/db", databaseRoutes); // POST requests
//When a user connects to chat room
io.on("connection", (socket) => {
//When a user joins chat room
socket.on("join-room", ({ id, username }) => {
const userDataOnJoin = userJoin(socket.id, username, id);
socket.join(userDataOnJoin.room);
socket.emit("message", formatMessage(chatBot, "Welcome to Seguro Chatting!"));
socket.broadcast.to(userDataOnJoin.room).emit("message", formatMessage(chatBot, `${userDataOnJoin.name} joined the chat`));
});
//Listen For Chat Messages
socket.on("chat-message", (message) => {
const userDataOnMessage = getCurrentUser(socket.id);
saveMessage(userDataOnMessage.room, formatMessage(userDataOnMessage.name, message));
io.to(userDataOnMessage.room).emit("message", formatMessage(userDataOnMessage.name, message));
});
//Runs when a user clicks save chat
socket.on("save-chat", () => {
saveChat(socket.id);
});
//Runs When Client Disconnects
socket.on("disconnect", () => {
const userDataOnLeave = userLeave(socket.id);
console.log(userDataOnLeave)
if (userDataOnLeave) {
io.to(userDataOnLeave.room).emit("message", formatMessage(chatBot, `${userDataOnLeave.name} left the chat`));
}
});
});
//Setting server to listen on port 'PORT'
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});