ws

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handle

func Handle(handler Handler) gin.HandlerFunc

Handle WebSocket 处理中间件

func HandleFunc

func HandleFunc(fn HandlerFunc) gin.HandlerFunc

HandleFunc 使用函数处理 WebSocket

func SetCheckOrigin

func SetCheckOrigin(fn func(r *http.Request) bool)

SetCheckOrigin 设置 Origin 检查函数

Types

type Connection

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

Connection WebSocket 连接

func NewConnection

func NewConnection(conn *websocket.Conn) *Connection

NewConnection 创建 WebSocket 连接

func Upgrade

func Upgrade(c *gin.Context) (*Connection, error)

Upgrade 升级 HTTP 连接为 WebSocket

func (*Connection) Close

func (c *Connection) Close()

Close 关闭连接

func (*Connection) IsClosed

func (c *Connection) IsClosed() bool

IsClosed 检查连接是否已关闭

func (*Connection) Send

func (c *Connection) Send(data []byte) error

Send 发送消息

func (*Connection) SendJSON

func (c *Connection) SendJSON(v any) error

SendJSON 发送 JSON 消息

func (*Connection) SendText

func (c *Connection) SendText(text string) error

SendText 发送文本消息

func (*Connection) SetReadDeadline

func (c *Connection) SetReadDeadline(t time.Time) error

SetReadDeadline 设置读取超时

func (*Connection) SetWriteDeadline

func (c *Connection) SetWriteDeadline(t time.Time) error

SetWriteDeadline 设置写入超时

type DefaultHandler

type DefaultHandler struct {
	OnConnectFunc func(conn *Connection)
	OnMessageFunc func(conn *Connection, message []byte)
	OnCloseFunc   func(conn *Connection)
	OnErrorFunc   func(conn *Connection, err error)
}

DefaultHandler 默认处理器

func (*DefaultHandler) OnClose

func (h *DefaultHandler) OnClose(conn *Connection)

func (*DefaultHandler) OnConnect

func (h *DefaultHandler) OnConnect(conn *Connection)

func (*DefaultHandler) OnError

func (h *DefaultHandler) OnError(conn *Connection, err error)

func (*DefaultHandler) OnMessage

func (h *DefaultHandler) OnMessage(conn *Connection, message []byte)

type Handler

type Handler interface {
	OnConnect(conn *Connection)
	OnMessage(conn *Connection, message []byte)
	OnClose(conn *Connection)
	OnError(conn *Connection, err error)
}

Handler WebSocket 处理器接口

type HandlerFunc

type HandlerFunc func(conn *Connection, message []byte)

HandlerFunc 处理函数类型

type Hub

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

Hub 连接管理中心(用于广播)

func NewHub

func NewHub() *Hub

NewHub 创建 Hub

func (*Hub) Broadcast

func (h *Hub) Broadcast(message []byte)

Broadcast 广播消息

func (*Hub) BroadcastJSON

func (h *Hub) BroadcastJSON(v any) error

BroadcastJSON 广播 JSON 消息

func (*Hub) Count

func (h *Hub) Count() int

Count 获取连接数

func (*Hub) Register

func (h *Hub) Register(conn *Connection)

Register 注册连接

func (*Hub) Run

func (h *Hub) Run()

Run 运行 Hub

func (*Hub) Unregister

func (h *Hub) Unregister(conn *Connection)

Unregister 注销连接

type Message

type Message struct {
	Type    MessageType `json:"type"`
	Content any         `json:"content"`
}

Message WebSocket 消息

type MessageType

type MessageType string

MessageType 消息类型

const (
	TypeText   MessageType = "text"
	TypeBinary MessageType = "binary"
	TypePing   MessageType = "ping"
	TypePong   MessageType = "pong"
	TypeClose  MessageType = "close"
)

Jump to

Keyboard shortcuts

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