-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (69 loc) · 2.15 KB
/
app.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
const express = require("express");
const path = require("path");
const http = require("http");
const cors = require("cors");
const redis = require("redis");
const MAX_MESSAGES = 30000;
const redisClient = redis.createClient({
host: "localhost",
port: 6379,
});
redisClient.on("error", function (error) {
console.error(`Redis error: ${error}`);
});
redisClient.connect();
const app = express();
app.set("port", process.env.PORT || 2222);
app.use(cors());
app.use(express.static(path.join(__dirname, "public")));
const server = http.createServer(app).listen(app.get("port"), () => {
console.log("server listening port " + app.get("port"));
});
const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: true,
},
});
io.listen(server);
let users = [];
io.on("connection", (socket) => {
const messages = getRedisMessages();
io.emit("allMessages", messages);
io.emit("userlist", users+1);
socket.on("disconnect", () => {
console.log(socket.nickname + "has disconnected");
const updateUsers = users.filter((user) => user != socket.nickname);
users = updateUsers;
io.emit("userlist", users);
});
// socket.on("nick", (nickname) => {
// socket.nickname = nickname;
// users.push(nickname);
// console.log("server : users :", users);
// io.emit("userlist", users);
// });
socket.on("chat", (data) => {
console.log(data)
const messageData = {
user: data.user,
message: data.message,
timestamp: new Date().toLocaleString(),
};
redisClient.rPush("messages", JSON.stringify(messageData)).then(() => {
redisClient.lLen("messages").then((length) => {
if (length > MAX_MESSAGES) {
redisClient.lTrim("messages", length - MAX_MESSAGES, -1);
}
});
});
io.emit("chat", {...messageData, user: `${messageData.user.charAt(0)}***${messageData.user.charAt(messageData.user.length-1)}`});
});
});
function getRedisMessages() {
redisClient.lRange("messages", 0, -1).then((messages) => {
const parsedMessages = messages.map((message) => JSON.parse(message));
io.emit("allMessages", parsedMessages);
});
}