-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
131 lines (112 loc) · 4.99 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
const express = require("express");
const path = require("path");
const app = express();
const fs = require("fs");
const server = require("http").createServer(app);
const io = require('socket.io')(server);
app.use(express.static(path.join(__dirname, "/public")));
const chatHistoryFilePath = path.join(__dirname, "chatHistory.txt");
const userPasswords = {}; // Store user passwords
function readChatHistoryFromFile() {
try {
const data = fs.readFileSync(chatHistoryFilePath, "utf8");
return JSON.parse(data);
} catch (error) {
console.error("Error reading chat history file:", error.message);
return [];
}
}
function writeChatHistoryToFile(chatHistory) {
try {
const data = JSON.stringify(chatHistory, null, 2);
fs.writeFileSync(chatHistoryFilePath, data, "utf8");
} catch (error) {
console.error("Error writing chat history file:", error.message);
}
}
const chatHistory = readChatHistoryFromFile();
io.on("connection", function (socket) {
socket.on("newuser", function (userInfo) {
const storedPassword = userPasswords[userInfo.username];
if (storedPassword) {
if (!userInfo.password || storedPassword !== userInfo.password) {
// Password mismatch or not provided, display error message and do not proceed
socket.emit("update", 'Wrong password, kindly retry.');
return;
}
} else {
// This is the first time the user is logging in, store the password
userPasswords[userInfo.username] = userInfo.password;
}
const existingUser = chatHistory.find(user => user.username === userInfo.username);
if (existingUser) {
// Returning user, provide sorted chat history to the user
const sortedChatHistory = chatHistory.flatMap(user => user.chatHistory);
sortedChatHistory.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
socket.emit("chatHistory", sortedChatHistory);
} else {
// New user, initialize chat history
const newUser = {
username: userInfo.username,
chatHistory: []
};
chatHistory.push(newUser);
writeChatHistoryToFile(chatHistory);
// Broadcast the new user to others
const systemMessage = {
username: 'system',
text: `${userInfo.username} joined the conversation`,
timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
};
io.emit("chat", systemMessage);
newUser.chatHistory.push(systemMessage);
writeChatHistoryToFile(chatHistory);
// Emit the entire sorted chat history to all users
const sortedChatHistory = chatHistory.flatMap(user => user.chatHistory);
sortedChatHistory.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
io.to(userInfo.username).emit("chatHistory", sortedChatHistory);
}
// Proceed to show the chat screen only if authentication is successful
socket.join(userInfo.username); // Join a room with the username
io.to(userInfo.username).emit("update", "You have joined the conversation.");
socket.on("chat", function (message) {
const user = chatHistory.find(u => u.username === userInfo.username);
if (user) {
if (!user.chatHistory) {
// Initialize chatHistory if not exists
user.chatHistory = [];
}
// Add timestamp to the message
message.timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
user.chatHistory.push(message);
writeChatHistoryToFile(chatHistory);
// Add user color and timestamp to the message before broadcasting
message.color = userInfo.color;
io.emit("chat", message);
}
});
// Handle user exit
socket.on("exituser", function () {
const systemMessage = {
username: 'system',
text: `${userInfo.username} left the conversation`,
timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
};
io.emit("chat", systemMessage);
});
// Handle disconnection (user closes the tab)
socket.on("disconnect", function () {
if (userInfo) {
const systemMessage = {
username: 'system',
text: `${userInfo.username} left the conversation`,
timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
};
io.emit("chat", systemMessage);
}
});
});
});
server.listen(5000, () => {
console.log('Server is running on http://localhost:5000');
});