Skip to content

Commit

Permalink
theme message first
Browse files Browse the repository at this point in the history
  • Loading branch information
kugarocks committed Sep 21, 2024
1 parent f0754d4 commit adb8fc5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
16 changes: 7 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,19 @@ <h1>Mini Chat</h1>
ws.onmessage = function(event) {
const data = JSON.parse(event.data);

if (data.type === 'message') {
addMessageToChat(data.user, data.text);
} else if (data.type === 'userList') {
updateUserList(data.users);
if (data.type === 'theme') {
currentTheme = data.theme;
console.log("Theme set to:", currentTheme);
} else if (data.type === 'username') {
username = data.username;
console.log("Assigned username:", username);
ws.send(JSON.stringify({ type: 'requestUserList' }));
// Update user list after username is assigned
updateUserList([username]);
} else if (data.type === 'userList') {
updateUserList(data.users);
} else if (data.type === 'message') {
addMessageToChat(data.user, data.text);
} else if (data.type === 'pong') {
console.log("Received pong");
} else if (data.type === 'theme') {
currentTheme = data.theme;
console.log("Theme set to:", currentTheme);
}
};

Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ func (h *Hub) run() {
select {
case client := <-h.register:
h.clients[client] = true
// First, send the theme message
client.send <- []byte(fmt.Sprintf(`{"type":"theme","theme":"%s"}`, h.theme))
// Then, assign username and send it
username := h.assignUsername(client)
h.broadcastUserList()
client.send <- []byte(fmt.Sprintf(`{"type":"username","username":"%s"}`, username))
client.send <- []byte(fmt.Sprintf(`{"type":"theme","theme":"%s"}`, h.theme))
// Log the successful username assignment
h.broadcastUserList()
log.Printf("Username assigned to client %s: %s", client.conn.RemoteAddr(), username)
case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
Expand Down

0 comments on commit adb8fc5

Please sign in to comment.