fix
Build and Publish / Metadata (push) Has been cancelled
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
2026-04-05 18:27:35 +02:00
parent 83c8f6eae2
commit d83fe85864
8 changed files with 72 additions and 8 deletions
+19 -1
View File
@@ -1,3 +1,4 @@
// Package websocket handles WebSocket connections and broadcasting telemetry updates to clients.
package websocket
import (
@@ -9,29 +10,35 @@ import (
"github.com/gorilla/websocket"
)
// DataType represents the type of telemetry data.
type DataType string
// Telemetry data types.
const (
DataTypeTraces DataType = "traces"
DataTypeMetrics DataType = "metrics"
DataTypeLogs DataType = "logs"
)
// HistoryProvider provides access to historical telemetry entries.
type HistoryProvider interface {
GetHistory(limit int) []HistoryEntry
}
// HistoryEntry represents a single archived telemetry entry.
type HistoryEntry struct {
Type string `json:"type"`
Timestamp int64 `json:"timestamp"`
Data interface{} `json:"data"`
}
// Entry represents a telemetry update sent to clients.
type Entry struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}
// Broadcaster interface for sending telemetry updates to all connected clients.
type Broadcaster interface {
Broadcast(data *Entry)
}
@@ -42,12 +49,14 @@ var upgrader = websocket.Upgrader{
},
}
// Server manages WebSocket client connections and broadcasts.
type Server struct {
clients map[*websocket.Conn]bool
mutex sync.Mutex
historyProvider HistoryProvider
}
// New creates a new WebSocket Server instance.
func New(historyProvider HistoryProvider) *Server {
return &Server{
@@ -56,13 +65,18 @@ func New(historyProvider HistoryProvider) *Server {
}
}
// HandleConnections upgrades HTTP requests to WebSocket and manages client lifecycle.
func (s *Server) HandleConnections(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("failed to upgrade connection: %v", err)
return
}
defer ws.Close()
defer func() {
if err := ws.Close(); err != nil {
log.Printf("failed to close connection: %v", err)
}
}()
s.mutex.Lock()
s.clients[ws] = true
@@ -98,6 +112,7 @@ func (s *Server) HandleConnections(w http.ResponseWriter, r *http.Request) {
log.Printf("client disconnected")
}
// Broadcast sends a telemetry entry to all connected clients.
func (s *Server) Broadcast(entry *Entry) {
s.mutex.Lock()
defer s.mutex.Unlock()
@@ -113,6 +128,7 @@ func (s *Server) Broadcast(entry *Entry) {
}
}
// SendHistory sends historical entries to a specific WebSocket client.
func (s *Server) SendHistory(ws *websocket.Conn) {
s.mutex.Lock()
defer s.mutex.Unlock()
@@ -131,6 +147,7 @@ func (s *Server) SendHistory(ws *websocket.Conn) {
}
}
// HandleMessage processes incoming messages from a WebSocket client.
func (s *Server) HandleMessage(ws *websocket.Conn, msg []byte) {
var message map[string]interface{}
if err := json.Unmarshal(msg, &message); err != nil {
@@ -148,6 +165,7 @@ func (s *Server) HandleMessage(ws *websocket.Conn, msg []byte) {
}
}
// SendPong sends a pong message back to a client.
func (s *Server) SendPong(ws *websocket.Conn) {
if err := ws.WriteMessage(websocket.PongMessage, nil); err != nil {
log.Printf("error sending pong: %v", err)