ws

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MsgConfig     = "config"
	MsgClipCreate = "clip:created"
	MsgClipExpire = "clip:expired"
	MsgClipList   = "clip:list"
	MsgPing       = "ping"
)

Message type constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents a single WebSocket connection managed by the Hub.

func (*Client) ReadPump

func (c *Client) ReadPump()

ReadPump reads messages from the WebSocket connection. On each received message it resets the read deadline. When the connection closes or the read deadline expires, it unregisters from the Hub.

func (*Client) WritePump

func (c *Client) WritePump()

WritePump writes messages from the send channel to the WebSocket connection.

type ClipListProvider

type ClipListProvider func() interface{}

ClipListProvider is a function that returns all current clips.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub manages all active WebSocket clients and broadcasts messages to them.

func NewHub

func NewHub(limits config.ServerLimits, readTimeout time.Duration, sendBufferSize int) *Hub

NewHub creates a Hub with the given server limits, read timeout, and send buffer size.

func (*Hub) Broadcast

func (h *Hub) Broadcast(msg *Message)

Broadcast sends a message to all registered clients using non-blocking sends. Slow clients that can't keep up are skipped (their channel is not blocked).

func (*Hub) HandleWS

func (h *Hub) HandleWS(ctx *gin.Context)

HandleWS is the Gin handler that upgrades HTTP to WebSocket. It sends a "config" message as the first message containing ServerLimits, sets the read deadline, and starts the client read/write pumps.

func (*Hub) Register

func (h *Hub) Register(client *Client)

Register adds a client to the hub.

func (*Hub) SetClipListProvider

func (h *Hub) SetClipListProvider(fn ClipListProvider)

SetClipListProvider sets the function used to retrieve the current clip list.

func (*Hub) Unregister

func (h *Hub) Unregister(client *Client)

Unregister removes a client from the hub and closes its send channel.

type Message

type Message struct {
	Type string      `json:"type"`
	Data interface{} `json:"data"`
}

Message is the envelope for all WebSocket messages.

Jump to

Keyboard shortcuts

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