wsutil

package
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WsHandler

func WsHandler(opts *ws2.ExtensionOpts) fiber.Handler

WsHandler returns a Fiber handler that upgrades connections to WebSocket using gobwas/ws for the protocol framing.

Because Fiber is built on fasthttp (which does not expose a standard net/http ResponseWriter with Hijack support), we use fasthttp's native Hijack mechanism:

  1. Read the Sec-WebSocket-Key from the request.
  2. Tell fasthttp not to send its own response (HijackSetNoResponse).
  3. Register a HijackHandler that receives the raw net.Conn.
  4. Inside the HijackHandler, write the 101 Switching Protocols response manually (RFC 6455 opening handshake).
  5. Run the read/write loops with gobwas/ws/wsutil on the hijacked conn.

Types

type CloseEvent

type CloseEvent struct {
	Code   int
	Reason string
}

type DoneChan

type DoneChan chan bool

type EventType

type EventType string
const (
	ConnectedEvent    EventType = "connected"
	DisconnectedEvent EventType = "disconnected"
	MessageEvent      EventType = "message"
)

type ManagerMetrics

type ManagerMetrics struct {
	RunningGoroutines   int32
	TotalSockets        int
	TotalRooms          int
	TotalListeners      int
	SocketsPerRoomCount map[string]int
	SocketsPerRoom      map[string][]string
	TotalMessages       int64
	MessagesPerSecond   int
	SecondsElapsed      int
}

type SocketConnection

type SocketConnection struct {
	Id     string
	RoomId string
	Done   DoneChan
	Writer WriterChan
}

type SocketEvent

type SocketEvent struct {
	SessionId string
	RoomId    string
	Type      EventType
	Payload   map[string]any
}

type SocketManager

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

func NewSocketManager

func NewSocketManager(opts *opts.ExtensionOpts) *SocketManager

func SocketManagerFromCtx

func SocketManagerFromCtx(ctx *h.RequestContext) *SocketManager

func (*SocketManager) Add

func (manager *SocketManager) Add(roomId string, id string, writer WriterChan, done DoneChan)

func (*SocketManager) BroadcastText

func (manager *SocketManager) BroadcastText(roomId string, message string, predicate func(conn SocketConnection) bool)

func (*SocketManager) Close

func (manager *SocketManager) Close()

Close stops the background metrics goroutine. Call this during graceful shutdown to avoid leaking the goroutine started by StartMetrics.

func (*SocketManager) CloseWithMessage

func (manager *SocketManager) CloseWithMessage(id string, message string)

func (*SocketManager) Disconnect

func (manager *SocketManager) Disconnect(id string)

func (*SocketManager) ForEachSocket

func (manager *SocketManager) ForEachSocket(roomId string, cb func(conn SocketConnection))

func (*SocketManager) Get

func (manager *SocketManager) Get(id string) *SocketConnection

func (*SocketManager) Listen

func (manager *SocketManager) Listen(listener chan SocketEvent)

func (*SocketManager) Metrics

func (manager *SocketManager) Metrics() ManagerMetrics

func (*SocketManager) OnClose

func (manager *SocketManager) OnClose(id string)

func (*SocketManager) OnMessage

func (manager *SocketManager) OnMessage(id string, message map[string]any)

func (*SocketManager) Ping

func (manager *SocketManager) Ping(id string) bool

func (*SocketManager) RunIntervalWithSocket

func (manager *SocketManager) RunIntervalWithSocket(socketId string, interval time.Duration, cb func() bool)

func (*SocketManager) SendHtml

func (manager *SocketManager) SendHtml(id string, message string) bool

func (*SocketManager) SendText

func (manager *SocketManager) SendText(id string, message string) bool

func (*SocketManager) StartMetrics

func (manager *SocketManager) StartMetrics()

type WriterChan

type WriterChan chan string

Jump to

Keyboard shortcuts

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