feat(server): restructure Pomodoro server into modular components
- move server logic to cmd/server/main.go
- create packages for websocket, pomodoro and broadcast handling
- define models for messages
- remove old GoTomato.go file
- update README
🤖
This commit is contained in:
parent
6d73711341
commit
c59f737eb7
10 changed files with 224 additions and 157 deletions
internal/broadcast
28
internal/broadcast/broadcast.go
Normal file
28
internal/broadcast/broadcast.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package broadcast
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gorilla/websocket"
|
||||
"log"
|
||||
)
|
||||
|
||||
// BroadcastMessage sends a message to all connected WebSocket clients.
|
||||
func BroadcastMessage(clients map[*websocket.Conn]bool, message models.BroadcastMessage) {
|
||||
// Marshal the message into JSON format
|
||||
jsonMessage, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling message: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Iterate over all connected clients and broadcast the message
|
||||
for client := range clients {
|
||||
err := client.WriteMessage(websocket.TextMessage, jsonMessage)
|
||||
if err != nil {
|
||||
log.Printf("Error broadcasting to client: %v", err)
|
||||
client.Close()
|
||||
delete(clients, client) // Remove the client if an error occurs
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue