ws

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const BufSize = 1024

Variables

View Source
var ErrClosed = errors.New("websocket connection closed")

Functions

func AsWebSocket

func AsWebSocket(handler func(*Conn) error) httpx.RespOption

AsWebSocket 设置为 WebSocket 连接

func AsWebSocketWithPing

func AsWebSocketWithPing(handler func(*Conn) error) httpx.RespOption

AsWebSocketWithPing 带 Ping 的 WebSocket

Types

type Conn

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

Conn is a WebSocket connection wrapper.

func NewConn

func NewConn(ctx *gin.Context) (*Conn, error)

NewConn creates a WebSocket connection by upgrading the HTTP request.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed reports whether the connection is closed.

func (*Conn) Messages

func (c *Conn) Messages() <-chan *Message

Messages returns a read-only channel of incoming messages.

func (*Conn) Send

func (c *Conn) Send(msg *Message) error

Send writes a message to the connection.

func (*Conn) SendError

func (c *Conn) SendError(errMsg string) error

SendError sends an error event.

func (*Conn) SendEvent

func (c *Conn) SendEvent(eventType string, data interface{}) error

SendEvent sends a typed event.

func (*Conn) SendJSON

func (c *Conn) SendJSON(data interface{}) error

SendJSON sends data as a "message"-typed event.

type Message

type Message struct {
	Type string      `json:"type"`
	Data interface{} `json:"data"`
	ID   string      `json:"id,omitempty"`
	Time int64       `json:"time,omitempty"`
}

Message represents a WebSocket message.

Jump to

Keyboard shortcuts

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