-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (106 loc) · 3.48 KB
/
app.js
File metadata and controls
110 lines (106 loc) · 3.48 KB
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
const express = require("express");
const { register } = require("module");
const fs = require("fs");
const path = require("path");
const { Socket } = require("socket.io");
const app = express();
const PORT = process.env.PORT || 4000;
const server = app.listen(PORT, () => console.log(`server on port${PORT}`));
const { Server } = require("socket.io");
const io = new Server(server);
let socketsConnected = new Set(); // Track connected users using a Set to store socket IDs
app.use(express.json()); // Middleware to parse JSON bodies
app.post("/register", (req, res) => {
console.log(req);
const userData = req.body;
fs.readFile(path.join(__dirname, "user.json"), "utf8", (err, data) => {
if (err) {
console.error("Error reading user.json", err);
return res.static(500).send("server Error");
}
let users;
try {
users = JSON.parse(data);
} catch (e) {
users = [];
}
users.push(userData);
fs.writeFile(
path.join(__dirname, "user.json"),
JSON.stringify(users),
(err) => {
if (err) {
console.error("error while writing on a file");
return res.status(500).send("Server error");
}
res.status(201).send("user registered successfully");
}
);
});
});
//Login handled but checking the json file and responcing to the clint and redirecting to the chat page is not done
app.post("/login", (req, res) => {
const { useremail, userpassword } = req.body;
fs.readFile(path.join(__dirname, "user.json"), "utf8", (err, data) => {
if (err) {
console.error("Error reading user.json", err);
return res.static(500).send("server Error");
}
let users;
try {
users = JSON.parse(data);
} catch (e) {
users = [];
}
const user = users.find(
(user) =>
user.useremail === useremail && user.userpassword === userpassword
);
if (user) {
return res.status(200).json({ message: "Login successfull", user });
} else {
return res.status(400).json({ message: "Invalid email or password" });
}
});
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "login.html"));
});
app.get("/register", (req, res) => {
res.sendFile(path.join(__dirname, "public", "register.html"));
});
app.use(express.static(path.join(__dirname, "public"))); // Serve static files from the "public" directory
io.on("connection", onConnected); // Listen for incoming socket connections
function onConnected(socket) {
console.log(`user has connected of id ${socket.id}`);
socketsConnected.add(socket.id); // Add new socket to the set of connected users
socket.on("disconnect", () => onDisconnect(socket)); // Handle socket disconnection
//client send message to the socket using emit
socket.on("chat message", (data) => {
console.log(data.dateTime);
//sending every websocket about the chat message
io.emit("chat message", {
typer: data.typer,
msg: data.message,
dateTime: new Date(data.dateTime),
id: socket.id,
});
});
var usersNum = socketsConnected.size;
console.log(usersNum);
io.emit("user count", usersNum);
socket.on("typing", (data) => {
//sending typing msg to all websockets
io.emit("typing", {
feedback: data.feedback,
id: data.id,
});
});
}
function onDisconnect(socket) {
console.log("user has disconnected", socket.id);
socketsConnected.delete(socket.id);
var usersNum = socketsConnected.size;
console.log(usersNum);
io.emit("user count", usersNum);
}