feat: add server_restart WebSocket message type for update notifications

This commit is contained in:
jevb
2026-03-14 21:58:18 +01:00
parent aa2a1cf025
commit 8ab2c93f1e
3 changed files with 47 additions and 0 deletions
+7
View File
@@ -98,6 +98,13 @@ func (h *Hub) BroadcastToAll(msg []byte) {
h.broadcast <- broadcastMsg{channelID: 0, msg: msg}
}
// BroadcastServerRestart sends a server_restart message to all connected clients.
// reason describes why the server is restarting (e.g., "update").
// delaySeconds tells clients how long until the server actually shuts down.
func (h *Hub) BroadcastServerRestart(reason string, delaySeconds int) {
h.BroadcastToAll(buildServerRestartMsg(reason, delaySeconds))
}
// SendToUser delivers msg directly to the client identified by userID.
// Returns true if the client was found and the message was queued.
func (h *Hub) SendToUser(userID int64, msg []byte) bool {
+11
View File
@@ -178,6 +178,17 @@ func buildSoundboardPlay(soundID string, userID int64) []byte {
})
}
// buildServerRestartMsg constructs a server_restart broadcast.
func buildServerRestartMsg(reason string, delaySeconds int) []byte {
return buildJSON(map[string]interface{}{
"type": "server_restart",
"payload": map[string]interface{}{
"reason": reason,
"delay_seconds": delaySeconds,
},
})
}
// parseChannelID safely extracts channel_id from a raw payload map.
func parseChannelID(payload json.RawMessage) (int64, error) {
var p struct {
+29
View File
@@ -0,0 +1,29 @@
package ws
import (
"encoding/json"
"testing"
)
func TestBuildServerRestartMsg(t *testing.T) {
msg := buildServerRestartMsg("update", 5)
var env struct {
Type string `json:"type"`
Payload struct {
Reason string `json:"reason"`
DelaySeconds int `json:"delay_seconds"`
} `json:"payload"`
}
if err := json.Unmarshal(msg, &env); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if env.Type != "server_restart" {
t.Errorf("type = %q, want server_restart", env.Type)
}
if env.Payload.Reason != "update" {
t.Errorf("reason = %q, want update", env.Payload.Reason)
}
if env.Payload.DelaySeconds != 5 {
t.Errorf("delay_seconds = %d, want 5", env.Payload.DelaySeconds)
}
}