Skip to content

Commit

Permalink
Adds request factory to WS backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed May 18, 2024
1 parent 0477a1e commit b0075fa
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions backend/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import (
type WSBackend struct {
connections map[string]*websocket.Conn
lock *sync.RWMutex
factory WSRequestFactory
URL string
}

type WSRequestFactory func(r wasabi.Request) (websocket.MessageType, []byte, error)

// NewWSBackend creates a new instance of WSBackend with the specified URL.
func NewWSBackend(url string) *WSBackend {
func NewWSBackend(url string, factory WSRequestFactory) *WSBackend {
return &WSBackend{
connections: make(map[string]*websocket.Conn),
lock: &sync.RWMutex{},
factory: factory,
URL: url,
}
}
Expand All @@ -33,8 +37,13 @@ func (b *WSBackend) Handle(conn wasabi.Connection, r wasabi.Request) error {
return err
}

// TODO: find a way to pass correct message type
return c.Write(r.Context(), websocket.MessageText, r.Data())
msgType, data, err := b.factory(r)

if err != nil {
return err
}

return c.Write(r.Context(), msgType, data)
}

// getConnection returns the websocket connection associated with the given connection.
Expand Down

0 comments on commit b0075fa

Please sign in to comment.