-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (72 loc) · 2.25 KB
/
Copy pathserver.js
File metadata and controls
90 lines (72 loc) · 2.25 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
const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static("public"));
const users = {};
const lastMessage = {};
function time() {
return new Date().toLocaleTimeString("tr-TR", {
hour: "2-digit",
minute: "2-digit"
});
}
io.on("connection", (socket) => {
console.log("Bağlandı:", socket.id);
// 🔥 Kullanıcı adı kontrol sistemi (BÜYÜK/KÜÇÜK HARF DUYARSIZ)
socket.on("check_username", (username) => {
if (!username) return;
const cleanName = username.trim().toLowerCase();
const nameExists = Object.values(users)
.some(u => u.toLowerCase() === cleanName);
if (nameExists) {
socket.emit("username_taken");
} else {
users[socket.id] = username.trim(); // Orijinal hali saklanır
socket.emit("join_success", username.trim());
socket.broadcast.emit("system", {
text: `${username.trim()} sohbete katıldı`,
time: time()
});
}
});
socket.on("chat", (msg) => {
const user = users[socket.id];
if (!user) {
socket.emit("system", {
text: "⚠️ Önce kullanıcı adıyla giriş yapmalısın",
time: time()
});
return;
}
const now = Date.now();
if (lastMessage[socket.id] && now - lastMessage[socket.id] < 1500) {
socket.emit("system", {
text: "⚠️ Çok hızlı yazıyorsun",
time: time()
});
return;
}
lastMessage[socket.id] = now;
io.emit("chat", {
user,
text: msg,
time: time()
});
});
socket.on("disconnect", () => {
const user = users[socket.id];
if (user) {
io.emit("system", {
text: `${user} ayrıldı`,
time: time()
});
delete users[socket.id];
}
});
});
server.listen(3000, () => {
console.log("Server Yazışmayla Başlıyor → http://localhost:3000");
});