Documentation ¶
Index ¶
- func RenderComponent(component templ.Component, ctx context.Context, buf *bytes.Buffer) (string, error)
- func RenderComponentToString(component templ.Component, ctx context.Context) (string, error)
- type Event
- type HTMXHeader
- type Publisher
- type Room
- type RoomList
- type SubRemovedCallback
- type Subscriber
- type SubscriberList
- type Votes
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Event ¶
type Event struct { Header HTMXHeader `json:"HEADERS"` Payload string `json:"payload"` }
Represent an HTMX websocket event
type HTMXHeader ¶
type Publisher ¶
type Publisher struct { //List of all subscriptions to a producer Subs SubscriberList //Used to lock before editing subs //As maps are not concurrent safe sync.RWMutex // contains filtered or unexported fields }
Publisher manages a set of Subscribers and broadcasts messages to them. It uses a RWMutex to allow concurrent reads from Subscribers while protecting data during writes.
func NewPublisher ¶
func NewPublisher() *Publisher
NewPublisher creates a new Publisher instance. It initializes the Subs field to an empty SubscriberList.
func (*Publisher) AddCallback ¶
func (p *Publisher) AddCallback(callback SubRemovedCallback)
func (*Publisher) AddSubscriber ¶
func (p *Publisher) AddSubscriber(sub *Subscriber)
AddSubscriber adds the given Subscriber to the Publisher's subscriber list. It acquires a write lock on the Publisher before modifying the list.
func (*Publisher) Broadcast ¶
Broadcast publishes the given data string to all subscribers. It acquires a write lock on the Publisher to synchronize access to the subscriber list during the broadcast. For each subscriber, it sends the data string over the subscriber's egress channel.
func (*Publisher) RemoveSubscriber ¶
func (p *Publisher) RemoveSubscriber(sub *Subscriber)
RemoveSubscriber removes the given Subscriber from the Publisher's subscriber list. It acquires a write lock, checks if the subscriber is in the list, closes the subscriber's connection if found, and deletes the subscriber from the list.
type Room ¶
type Room struct { Id string `json:"id"` RoomName string `json:"roomName"` Pub *Publisher `json:"-"` VoteMap *Votes `json:"_"` VotesReveledFlag bool }
Room represents a room in the server (or session) Each room will have one Publisher a Publisher can have one or more subscribers A Room hold the VoteMap used to store all subscribers names maped to their vote
func (*Room) OnSubRemoved ¶
func (r *Room) OnSubRemoved(subscriber *Subscriber)
OnSubRemoved will update vote map for all users
type RoomList ¶
func NewRoomList ¶
func NewRoomList() *RoomList
type SubRemovedCallback ¶
type SubRemovedCallback interface {
OnSubRemoved(subscriber *Subscriber)
}
type Subscriber ¶
type Subscriber struct { Publisher *Publisher // contains filtered or unexported fields }
func NewSubscriber ¶
func NewSubscriber(conn *websocket.Conn, room *Room, displayName string) *Subscriber
func (*Subscriber) ReadMessage ¶
func (s *Subscriber) ReadMessage(room *Room)
ReadMessage Reads a websocket message setting read limits and pong handler
- SetReadDeadline to pongWait from now + pongWait
- SetReadLimit to 512 bytes (max message size) to avoid large messages
- SetPongHandler to pongHandler to handle pong messages
- Read messages from the websocket connection
- If the message of type Event then update the voteMap to the Event payload and broadcast updated voteMap to all subscribers
- If the message is a close message or error then close the connection
func (*Subscriber) WriteMessages ¶
func (s *Subscriber) WriteMessages()
WriteMessages writes messages to the websocket connection 1. Sets a ticker interval to pong the client 2. Reads messages from the egress channel 3. Sends messages to the client
type SubscriberList ¶
type SubscriberList map[*Subscriber]bool
SubscriberList Used to help manage subscribers