server

package
v0.0.0-...-37de97e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderComponent

func RenderComponent(component templ.Component, ctx context.Context, buf *bytes.Buffer) (string, error)

RenderComponent renders the provided component to the given bytes.Buffer using the context. It returns the rendered content as a string and any error.

func RenderComponentToString

func RenderComponentToString(component templ.Component, ctx context.Context) (string, error)

RenderComponentToString renders a templ component and returns as string

Types

type Event

type Event struct {
	Header  HTMXHeader `json:"HEADERS"`
	Payload string     `json:"payload"`
}

Represent an HTMX websocket event

type HTMXHeader

type HTMXHeader struct {
	HXRequest     string `json:"HX-Request"`
	HXTrigger     string `json:"HX-Trigger"`
	HXTriggerName string `json:"HX-Trigger-Name"`
	HXTarget      string `json:"HX-Target"`
	HXCurrentURL  string `json:"HX-Current-URL"`
}

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

func (p *Publisher) Broadcast(data string)

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 NewRoom

func NewRoom(name string) *Room

func (*Room) OnSubRemoved

func (r *Room) OnSubRemoved(subscriber *Subscriber)

OnSubRemoved will update vote map for all users

func (*Room) Size

func (r *Room) Size() string

type RoomList

type RoomList struct {
	Rooms map[*Room]bool
	sync.RWMutex
}

func NewRoomList

func NewRoomList() *RoomList

func (*RoomList) Is

func (r *RoomList) Is(id string) (*Room, bool)

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

  1. SetReadDeadline to pongWait from now + pongWait
  2. SetReadLimit to 512 bytes (max message size) to avoid large messages
  3. SetPongHandler to pongHandler to handle pong messages
  4. Read messages from the websocket connection
  5. If the message of type Event then update the voteMap to the Event payload and broadcast updated voteMap to all subscribers
  6. 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

type Votes

type Votes struct {
	VoteMap map[string]string
	sync.Mutex
}

Votes Represents the object to store user votes

func NewVoteMap

func NewVoteMap() *Votes

func (*Votes) Remove

func (v *Votes) Remove(name string)

Remove a user from the vote map

func (*Votes) SortNames

func (v *Votes) SortNames() []string

Function reurn list of sorted names alphabetically

func (*Votes) Update

func (v *Votes) Update(name string, vote string)

Update a vote map to add a user with a vote

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL