Add user prefix.

This commit is contained in:
emil28092005
2026-01-14 02:48:16 +03:00
parent 975aea12c9
commit ad38c6aeed
6 changed files with 39 additions and 16 deletions
+2 -1
View File
@@ -1 +1,2 @@
project_context.txt
project_context.txt
shagram.db
+7 -3
View File
@@ -39,14 +39,18 @@ func WebSocketHandler(hub *websocket.Hub, database *db.DB) gin.HandlerFunc {
if err != nil {
break
}
user := msg["user"]
if user == "" {
user = "Anonymous"
}
_, err = database.Exec(`
INSERT INTO messages (room_id, user, text)
VALUES (?, ?, ?)`, roomID, "user", msg["text"])
VALUES (?, ?, ?)`, roomID, user, msg["text"])
if err != nil {
log.Printf("Save message error: %v", err)
}
message := []byte(msg["text"])
formattedMsg := user + ": " + msg["text"]
message := []byte(formattedMsg)
room.Broadcast(message)
}
}()
+19 -8
View File
@@ -257,9 +257,13 @@ func WebSocketHandler(hub *websocket.Hub, database *db.DB) gin.HandlerFunc {
if err != nil {
break
}
user := msg["user"]
if user == "" {
user = "Anonymous"
}
_, err = database.Exec(`
INSERT INTO messages (room_id, user, text)
VALUES (?, ?, ?)`, roomID, "user", msg["text"])
VALUES (?, ?, ?)`, roomID, user, msg["text"])
if err != nil {
log.Printf("Save message error: %v", err)
}
@@ -415,7 +419,7 @@ CREATE TABLE IF NOT EXISTS messages (
INSERT OR IGNORE INTO rooms (id, name) VALUES ('general', 'General');
--- shagram.db ---
SQLite format 3@ ø nÍ] P++Ytablesqlite_sequencesqlite_sequenceCREATE TABLE sqlite_sequence(name,seq)ƒotablemessagesmessagesCREATE TABLE messages (
SQLite format 3@ ø nÍ] P++Ytablesqlite_sequencesqlite_sequenceCREATE TABLE sqlite_sequence(name,seq)ƒotablemessagesmessagesCREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id TEXT NOT NULL,
user TEXT NOT NULL,
@@ -427,8 +431,8 @@ SQLite format 3@ 
name TEXT NOT NULL
))=indexsqlite_autoindex_rooms_1roomsíígeneralGeneral
õõ
 general␍ _Ñ¢[5 çÂyS-␍Ü␍³␍„␍Z␍/␍ Ø ¯ _$3chatuserAAA2026-01-12 21:59:44(3chatuserdfgsadf2026-01-12 21:59:35'3devusertest DB2026-01-12 21:58:42,#3chatusersefsefsefes2026-01-12 21:43:38'3chatusersefesf2026-01-12 21:43:35)3chatuserdfbzgdfg2026-01-12 21:43:26(3chatuserkuykfgh2026-01-12 21:42:52-3generaluserawfawdfwa2026-01-12 21:28:44'3devuserwadawdw2026-01-12 21:28:40&3chatuserefawe2026-01-12 21:28:16'␍3chatuserawdwad2026-01-12 21:27:58$ 3chatuserawd2026-01-12 21:27:25$ 3chatuseradw2026-01-12 21:27:23"
3chatuserd2026-01-12 21:27:20# 3chatuserda2026-01-12 21:27:16#3chatuserku2026-01-12 21:26:58#3chatuserhi2026-01-12 21:26:43'3generaluserASD2026-01-12 21:16:37$3chatuserASD2026-01-12 21:16:30"3devuserHI2026-01-12 21:16:08!3devuser12026-01-12 20:59:23-'3devuserHi from DB! 22026-01-12 20:58:43-'3devuserHi from DB! 12026-01-12 20:58:34␍òò messages
 general␍ 2Ñ¢[5 çÂyS-␍Ü␍³␍„␍Z␍/␍ Ø ¯ _ 2+!3chatЭмильку2026-01-13 23:23:54$3chatuserAAA2026-01-12 21:59:44(3chatuserdfgsadf2026-01-12 21:59:35'3devusertest DB2026-01-12 21:58:42,#3chatusersefsefsefes2026-01-12 21:43:38'3chatusersefesf2026-01-12 21:43:35)3chatuserdfbzgdfg2026-01-12 21:43:26(3chatuserkuykfgh2026-01-12 21:42:52-3generaluserawfawdfwa2026-01-12 21:28:44'3devuserwadawdw2026-01-12 21:28:40&3chatuserefawe2026-01-12 21:28:16'␍3chatuserawdwad2026-01-12 21:27:58$ 3chatuserawd2026-01-12 21:27:25$ 3chatuseradw2026-01-12 21:27:23"
3chatuserd2026-01-12 21:27:20# 3chatuserda2026-01-12 21:27:16#3chatuserku2026-01-12 21:26:58#3chatuserhi2026-01-12 21:26:43'3generaluserASD2026-01-12 21:16:37$3chatuserASD2026-01-12 21:16:30"3devuserHI2026-01-12 21:16:08!3devuser12026-01-12 20:59:23-'3devuserHi from DB! 22026-01-12 20:58:43-'3devuserHi from DB! 12026-01-12 20:58:34␍òò messages
--- static/index.html ---
<!DOCTYPE html>
<html>
@@ -453,21 +457,22 @@ SQLite format 3@ 
}
input { padding: 8px; width: 300px; }
button { padding: 8px 15px; cursor: pointer; }
select { padding: 8px; width: 320px; } /* â† Ð´Ð»Ñ ÐºÐ¾Ð¼Ð½Ð°Ñ‚ */
select { padding: 8px; width: 320px; }
#roomsList { margin: 10px 0; }
</style>
</head>
<body>
<h1>📱 Shagram Chat</h1>
<!-- ↠ÐОВЫЙ БЛОК С КОМÐÐТÐМИ -->
<div id="roomsList">
<select id="roomSelect">
<option value="general">general</option>
</select>
<button onclick="loadRooms()">🔄 Refresh</button>
<input type="text" id="userInput" placeholder="Your name" value="Эмиль" maxlength="20">
<button onclick="loadRooms()">🔄</button>
<button onclick="connectRoom()">Connect</button>
</div>
<div id="messages"></div>
@@ -572,12 +577,18 @@ function connectRoom() {
function sendMessage() {
const text = messageInput.value.trim();
const usernameInput = document.getElementById('userInput');
const username = usernameInput ? usernameInput.value.trim() || 'Anonymous' : 'Anonymous';
if (!text || !ws || ws.readyState !== WebSocket.OPEN) {
alert('Not connected or empty message');
return;
}
ws.send(JSON.stringify({text: text}));
ws.send(JSON.stringify({
text: text,
user: username,
}));
messageInput.value = '';
}
BIN
View File
Binary file not shown.
+4 -3
View File
@@ -21,21 +21,22 @@
}
input { padding: 8px; width: 300px; }
button { padding: 8px 15px; cursor: pointer; }
select { padding: 8px; width: 320px; } /* ← для комнат */
select { padding: 8px; width: 320px; }
#roomsList { margin: 10px 0; }
</style>
</head>
<body>
<h1>📱 Shagram Chat</h1>
<!-- ← НОВЫЙ БЛОК С КОМНАТАМИ -->
<div id="roomsList">
<select id="roomSelect">
<option value="general">general</option>
</select>
<button onclick="loadRooms()">🔄 Refresh</button>
<input type="text" id="userInput" placeholder="Your name" value="Эмиль" maxlength="20">
<button onclick="loadRooms()">🔄</button>
<button onclick="connectRoom()">Connect</button>
</div>
<div id="messages"></div>
+7 -1
View File
@@ -88,12 +88,18 @@ function connectRoom() {
function sendMessage() {
const text = messageInput.value.trim();
const usernameInput = document.getElementById('userInput');
const username = usernameInput ? usernameInput.value.trim() || 'Anonymous' : 'Anonymous';
if (!text || !ws || ws.readyState !== WebSocket.OPEN) {
alert('Not connected or empty message');
return;
}
ws.send(JSON.stringify({text: text}));
ws.send(JSON.stringify({
text: text,
user: username,
}));
messageInput.value = '';
}