Added messages history
This commit is contained in:
parent
fe3bc49a7c
commit
e18cb3854a
38
index.js
38
index.js
@ -30,15 +30,27 @@ function getCurrentTime() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var d = new Date();
|
var d = new Date();
|
||||||
|
|
||||||
|
var day = addZero(d.getUTCDate());
|
||||||
|
var month = addZero(d.getUTCMonth() + 1);
|
||||||
|
var year = d.getFullYear();
|
||||||
|
|
||||||
var h = addZero(d.getHours());
|
var h = addZero(d.getHours());
|
||||||
var m = addZero(d.getMinutes());
|
var m = addZero(d.getMinutes());
|
||||||
var s = addZero(d.getSeconds());
|
var s = addZero(d.getSeconds());
|
||||||
return h + ":" + m + ":" + s;
|
return "[" + day + "-"+ month + "-" + year + " " + h + ":" + m + ":" + s + "]";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function log(message) {
|
||||||
|
console.log(getCurrentTime() + " " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
usersConnected = 0;
|
usersConnected = 0;
|
||||||
lastUsername = 0;
|
lastUsername = 0;
|
||||||
|
messagesHistory = []
|
||||||
|
|
||||||
io.on("connection", (client) => {
|
io.on("connection", (client) => {
|
||||||
|
|
||||||
@ -48,39 +60,45 @@ io.on("connection", (client) => {
|
|||||||
client.on("disconnect", () => {
|
client.on("disconnect", () => {
|
||||||
usersConnected--;
|
usersConnected--;
|
||||||
io.sockets.emit("update-count", {value: usersConnected});
|
io.sockets.emit("update-count", {value: usersConnected});
|
||||||
console.log(getCurrentTime() + ": Client disconnection (" + client.usernameDenis + ")");
|
log(`${client.usernameDenis} has disconnected`);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("message", (data) => {
|
client.on("message", (data) => {
|
||||||
if(data.message.toLowerCase().includes("denis")) {
|
if(data.message.toLowerCase().includes("denis")) {
|
||||||
io.sockets.emit("new-message", {author: client.usernameDenis, message: data.message});
|
io.sockets.emit("new-message", {author: client.usernameDenis, message: data.message});
|
||||||
console.log(getCurrentTime() + ": (" + client.usernameDenis + ") : " + data.message);
|
log(`${client.usernameDenis} : ${data.message}`);
|
||||||
|
|
||||||
|
messagesHistory.push([client.usernameDenis, data.message]);
|
||||||
|
if(messagesHistory.length > 20) {
|
||||||
|
messagesHistory.shift();
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
client.emit("warning");
|
client.emit("warning");
|
||||||
console.log(getCurrentTime() + ": (" + client.usernameDenis + ") : " + data.message + "(invalid)");
|
log(`${client.usernameDenis} : ${data.message} (INVALID)`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
lastUsername++;
|
lastUsername++;
|
||||||
client.usernameDenis = "DenisFan" + lastUsername;
|
client.usernameDenis = "DenisFan" + lastUsername;
|
||||||
client.emit("username", {username: "DenisFan" + lastUsername});
|
client.emit("username", {username: "DenisFan" + lastUsername});
|
||||||
console.log(getCurrentTime() + ": Client connection (" + client.usernameDenis + ")");
|
log(`${client.usernameDenis} is now connected`);
|
||||||
|
|
||||||
|
client.emit("previous-messages", {messages: messagesHistory});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.use(express.static("src"));
|
app.use(express.static("src"));
|
||||||
|
|
||||||
|
|
||||||
app.listen(80, () => {
|
app.listen(80, () => {
|
||||||
console.log(getCurrentTime() + ": Web server started on port 80")
|
log("Web server started on port 80")
|
||||||
});
|
});
|
||||||
|
|
||||||
webHTTPSServer.listen(443, () => {
|
webHTTPSServer.listen(443, () => {
|
||||||
console.log(getCurrentTime() + ": Web HTTPS server started on port 443");
|
log("Web HTTPS server started on port 443");
|
||||||
});
|
});
|
||||||
|
|
||||||
socketHTTPSServer.listen(3000, () => {
|
socketHTTPSServer.listen(3000, () => {
|
||||||
console.log(getCurrentTime() + ": Socket HTTPS server started on port 3000");
|
log("Socket HTTPS server started on port 3000");
|
||||||
});
|
});
|
||||||
|
@ -36,6 +36,12 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
addMessage("Rappel", "Vous devez parler uniquement de denis !");
|
addMessage("Rappel", "Vous devez parler uniquement de denis !");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on("previous-messages", (data) => {
|
||||||
|
data.messages.forEach(message => {
|
||||||
|
addMessage(message[0], message[1]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
document.getElementById("value").innerText = "Serveur injoingnable !";
|
document.getElementById("value").innerText = "Serveur injoingnable !";
|
||||||
|
Loading…
Reference in New Issue
Block a user