websocket

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TextMessage   = websocket.TextMessage
	BinaryMessage = websocket.BinaryMessage
)

Variables

View Source
var (
	ErrReadTimeout  = errors.New("read timeout")
	ErrWriteTimeout = errors.New("write timeout")
)

Functions

func PeriodicWrite

func PeriodicWrite(c *gin.Context, interval time.Duration, get func() (any, error), deduplicate ...DeduplicateFunc)

Types

type DeduplicateFunc

type DeduplicateFunc func(last, current any) bool

type Manager

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

Manager handles WebSocket connection state and ping-pong

func NewManagerWithUpgrade

func NewManagerWithUpgrade(c *gin.Context) (*Manager, error)

NewManagerWithUpgrade upgrades the HTTP connection to a WebSocket connection and returns a Manager. If the upgrade fails, the error is returned. If the upgrade succeeds, the Manager is returned.

To use a custom upgrader, set the "upgrader" context value to the upgrader.

func (*Manager) Close

func (cm *Manager) Close()

Close closes the connection and cancels the context

func (*Manager) Context

func (cm *Manager) Context() context.Context

func (*Manager) Done

func (cm *Manager) Done() <-chan struct{}

Done returns a channel that is closed when the context is done or the connection is closed

func (*Manager) NewReader

func (m *Manager) NewReader() io.Reader

func (*Manager) NewWriter

func (cm *Manager) NewWriter(msgType int) io.Writer

func (*Manager) PeriodicWrite

func (cm *Manager) PeriodicWrite(interval time.Duration, getData func() (any, error), deduplicate ...DeduplicateFunc) error

Periodic writes data to the connection periodically, with deduplication. If the connection is closed, the error is returned. If the write timeout is reached, ErrWriteTimeout is returned.

func (*Manager) ReadBinary

func (cm *Manager) ReadBinary(timeout time.Duration) ([]byte, error)

func (*Manager) ReadJSON

func (cm *Manager) ReadJSON(out any, timeout time.Duration) error

ReadJSON reads a JSON message from the connection and unmarshals it into the provided struct with sonic If the connection is closed, the error is returned. If the message fails to unmarshal, the error is returned. If the read timeout is reached, ErrReadTimeout is returned.

func (*Manager) WriteData

func (cm *Manager) WriteData(typ int, data []byte, timeout time.Duration) error

WriteData writes a message to the connection with sonic. If the connection is closed, the error is returned. If the write timeout is reached, ErrWriteTimeout is returned.

func (*Manager) WriteJSON

func (cm *Manager) WriteJSON(data any, timeout time.Duration) error

WriteJSON writes a JSON message to the connection with json. If the connection is closed, the error is returned. If the write timeout is reached, ErrWriteTimeout is returned.

type Reader

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

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

type Writer

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

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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