-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (47 loc) · 1.75 KB
/
index.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
import express from "express";
import { fileURLToPath } from "url";
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import { Server } from "socket.io";
const app = express();
const server = app.listen(3000, () => {
console.log("Server is running on port 3000");
});
const io = new Server(server);
function getCurrentDateTime() {
const currentDate = new Date();
// Get date components
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, "0"); // Months are zero-based
const day = String(currentDate.getDate()).padStart(2, "0");
// Get time components
const hours = String(currentDate.getHours()).padStart(2, "0");
const minutes = String(currentDate.getMinutes()).padStart(2, "0");
const seconds = String(currentDate.getSeconds()).padStart(2, "0");
// Format the date and time
const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return formattedDateTime;
}
// Usage
// Serve HTML file
app.get("/", (req, res) => {
res.sendFile("index.html", { root: __dirname });
});
// Socket.io connection
io.on("connection", (socket) => {
const dateTimeString = getCurrentDateTime();
console.log("A user connected");
io.emit("chat message", `A user connected at: ${dateTimeString}`);
// Listen for chat messages
socket.on("chat message", (msg) => {
console.log("message: " + msg);
io.emit("chat message", msg); // Broadcast the message to all connected clients
});
// Listen for disconnection
socket.on("disconnect", () => {
const dateTimeString = getCurrentDateTime();
console.log("User disconnected");
io.emit("chat message", `A user disconnected at: ${dateTimeString}`);
});
});