mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-05-07 10:05:54 +00:00
work towards decoupling api from router
This commit is contained in:
73
events.go
73
events.go
@@ -1,64 +1,53 @@
|
||||
package showbridge
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Type string `json:"type"`
|
||||
Data any `json:"data,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (e Event) toJSON() ([]byte, error) {
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
func (r *Router) handleEvent(event Event, sender *websocket.Conn) {
|
||||
func (r *Router) HandleEvent(event common.Event, sender common.EventDestination) {
|
||||
switch event.Type {
|
||||
case "ping":
|
||||
r.unicastEvent(Event{Type: "pong"}, sender)
|
||||
r.unicastEvent(common.Event{Type: "pong", Data: map[string]any{
|
||||
"timestamp": time.Now().UnixMilli(),
|
||||
}}, sender)
|
||||
default:
|
||||
r.logger.Warn("unknown event type", "eventType", event.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) unicastEvent(event Event, conn *websocket.Conn) {
|
||||
eventJSON, err := event.toJSON()
|
||||
if err != nil {
|
||||
r.logger.Error("failed to marshal event to JSON", "error", err)
|
||||
return
|
||||
}
|
||||
err = conn.WriteMessage(websocket.TextMessage, eventJSON)
|
||||
if err != nil {
|
||||
r.logger.Error("failed to write message to websocket connection", "error", err)
|
||||
func (r *Router) AddEventDestination(dest common.EventDestination) {
|
||||
r.eventDestinationsMu.Lock()
|
||||
defer r.eventDestinationsMu.Unlock()
|
||||
r.eventDestinations = append(r.eventDestinations, dest)
|
||||
}
|
||||
|
||||
func (r *Router) RemoveEventDestination(dest common.EventDestination) {
|
||||
r.eventDestinationsMu.Lock()
|
||||
defer r.eventDestinationsMu.Unlock()
|
||||
for i, d := range r.eventDestinations {
|
||||
if d.Is(dest) {
|
||||
r.eventDestinations = append(r.eventDestinations[:i], r.eventDestinations[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) broadcastEvent(event Event, excluded ...*websocket.Conn) {
|
||||
eventJSON, err := event.toJSON()
|
||||
func (r *Router) unicastEvent(event common.Event, dest common.EventDestination) {
|
||||
err := dest.Send(event)
|
||||
if err != nil {
|
||||
r.logger.Error("failed to marshal event to JSON", "error", err)
|
||||
return
|
||||
r.logger.Error("failed to send event", "error", err)
|
||||
}
|
||||
r.wsConnsMu.Lock()
|
||||
defer r.wsConnsMu.Unlock()
|
||||
for _, conn := range r.wsConns {
|
||||
exclude := false
|
||||
for _, excludedConn := range excluded {
|
||||
if conn == excludedConn {
|
||||
exclude = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if exclude {
|
||||
continue
|
||||
}
|
||||
err := conn.WriteMessage(websocket.TextMessage, eventJSON)
|
||||
}
|
||||
|
||||
func (r *Router) broadcastEvent(event common.Event) {
|
||||
r.eventDestinationsMu.Lock()
|
||||
defer r.eventDestinationsMu.Unlock()
|
||||
for _, dest := range r.eventDestinations {
|
||||
err := dest.Send(event)
|
||||
if err != nil {
|
||||
r.logger.Error("failed to write message to websocket connection", "error", err)
|
||||
r.logger.Error("failed to send event", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user