Skip to content

Commit

Permalink
feat: broadcast total clients connected
Browse files Browse the repository at this point in the history
  • Loading branch information
ivynya committed Dec 25, 2023
1 parent ed3e145 commit 1ff0b18
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func main() {
// Log join message
fmt.Println("Client joined from " + c.RemoteAddr().String())
fmt.Println("Total clients:", len(clients))
c.WriteMessage(websocket.TextMessage, []byte(`{"action":"join","data":"`+strconv.Itoa(len(clients))+`"}`))
for client := range clients {
client.WriteMessage(websocket.TextMessage, []byte(`{"action":"join","data":"`+strconv.Itoa(len(clients))+`"}`))
}

for {
// Read message from client
Expand All @@ -51,12 +53,18 @@ func main() {
if err != nil {
log.Println("Websocket write error:", err)
delete(clients, client)
for client := range clients {
client.WriteMessage(websocket.TextMessage, []byte(`{"action":"join","data":"`+strconv.Itoa(len(clients))+`"}`))
}
}
}
}

// Unregister client
delete(clients, c)
for client := range clients {
client.WriteMessage(websocket.TextMessage, []byte(`{"action":"join","data":"`+strconv.Itoa(len(clients))+`"}`))
}
}))

// Start the server
Expand Down

0 comments on commit 1ff0b18

Please sign in to comment.