ws

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

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

Connection is a middleman between the websocket connection and the hub.

func (*Connection) Emit

func (c *Connection) Emit(event string, data any) error

Emit sends a message to this connection.

func (*Connection) Join

func (c *Connection) Join(room string)

Join joins a room.

func (*Connection) Leave

func (c *Connection) Leave(room string)

Leave leaves a room.

func (*Connection) On

func (c *Connection) On(event string, handler func(json.RawMessage))

On registers a handler for a specific event type.

type Hub

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

Hub manages active WebSocket connections and rooms.

func NewHub

func NewHub(redis redis.UniversalClient, rChan string) *Hub

NewHub creates a new Hub.

func (*Hub) BroadcastToRoom

func (h *Hub) BroadcastToRoom(room string, event string, data any) error

BroadcastToRoom sends a message to all connections in a specific room across all nodes.

func (*Hub) JoinRoom

func (h *Hub) JoinRoom(conn *Connection, room string)

JoinRoom adds a connection to a room.

func (*Hub) LeaveRoom

func (h *Hub) LeaveRoom(conn *Connection, room string)

LeaveRoom removes a connection from a room.

func (*Hub) Run

func (h *Hub) Run()

Run starts the hub loop and optionally the Redis subscription.

func (*Hub) Stop

func (h *Hub) Stop(ctx context.Context) error

Stop signals the hub to shut down.

type InboundMessage

type InboundMessage struct {
	Event string          `json:"event"`
	Data  json.RawMessage `json:"data"`
}

InboundMessage represents a JSON message from the client.

type SSEEvent

type SSEEvent struct {
	Event string
	Data  string
	ID    string
}

SSEEvent represents a single server-sent event.

type SSEServer

type SSEServer struct{}

SSEServer handles SSE connections.

func NewSSEServer

func NewSSEServer() *SSEServer

NewSSEServer creates a new SSE server.

func (*SSEServer) Handler

func (s *SSEServer) Handler(w http.ResponseWriter, r *http.Request, stream func(events chan<- SSEEvent))

Handler returns an HTTP handler for SSE.

type Upgrader

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

Upgrader handles upgrading HTTP requests to WebSockets.

func NewUpgrader

func NewUpgrader(hub *Hub, wsConfig config.WSConfig, isDev bool) *Upgrader

NewUpgrader creates a new WS upgrader.

func (*Upgrader) Upgrade

func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, userID string) (*Connection, error)

Upgrade upgrades the HTTP request to a WS connection.

Jump to

Keyboard shortcuts

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