websocket

package
v1.0.20250928075430 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 默认配置常量
	DefaultWriteWait       = 10 * time.Second
	DefaultPongWait        = 60 * time.Second
	DefaultPingPeriod      = (DefaultPongWait * 9) / 10
	DefaultMaxMessageSize  = 512
	DefaultReadBufferSize  = 1024
	DefaultWriteBufferSize = 1024
)

Variables

This section is empty.

Functions

func NewSimpleUpgrader

func NewSimpleUpgrader() *websocket.Upgrader

NewSimpleUpgrader 创建简单的WebSocket升级器

Types

type Config

type Config struct {
	ReadBufferSize  int           `json:"read_buffer_size"`  // 读缓冲区大小
	WriteBufferSize int           `json:"write_buffer_size"` // 写缓冲区大小
	WriteWait       time.Duration `json:"write_wait"`        // 写超时时间
	PongWait        time.Duration `json:"pong_wait"`         // Pong等待时间
	PingPeriod      time.Duration `json:"ping_period"`       // Ping发送间隔
	MaxMessageSize  int64         `json:"max_message_size"`  // 最大消息大小
	CheckOrigin     bool          `json:"check_origin"`      // 是否检查来源
}

Config WebSocket配置

type Connection

type Connection interface {
	// WriteMessage 写入消息
	WriteMessage(messageType int, data []byte) error
	// ReadMessage 读取消息
	ReadMessage() (messageType int, p []byte, err error)
	// WriteJSON 写入JSON消息
	WriteJSON(v interface{}) error
	// ReadJSON 读取JSON消息
	ReadJSON(v interface{}) error
	// Close 关闭连接
	Close() error
	// SetWriteDeadline 设置写超时
	SetWriteDeadline(t time.Time) error
	// SetReadDeadline 设置读超时
	SetReadDeadline(t time.Time) error
	// SetPongHandler 设置Pong处理器
	SetPongHandler(h func(appData string) error)
	// SetCloseHandler 设置关闭处理器
	SetCloseHandler(h func(code int, text string) error)
	// LocalAddr 获取本地地址
	LocalAddr() string
	// RemoteAddr 获取远程地址
	RemoteAddr() string
}

Connection WebSocket连接接口

type ConnectionHandler

type ConnectionHandler interface {
	// OnConnect 连接建立时调用
	OnConnect(conn Connection) error
	// OnMessage 收到消息时调用
	OnMessage(conn Connection, messageType int, data []byte) error
	// OnClose 连接关闭时调用
	OnClose(conn Connection, code int, text string) error
	// OnError 发生错误时调用
	OnError(conn Connection, err error) error
}

ConnectionHandler WebSocket连接处理器

type Manager

type Manager interface {
	// Upgrade 升级HTTP连接为WebSocket
	Upgrade(ctx *gin.Context, responseHeader http.Header) (Connection, error)
	// UpgradeHTTP 升级原生HTTP连接为WebSocket
	UpgradeHTTP(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (Connection, error)
	// StartHeartbeat 启动心跳检测
	StartHeartbeat(ctx context.Context, conn Connection) error
	// HandleConnection 处理WebSocket连接
	HandleConnection(ctx context.Context, conn Connection, handler ConnectionHandler) error
}

Manager WebSocket管理器接口

func NewManager

func NewManager(config *Config, logger *zap.Logger) Manager

NewManager 创建WebSocket管理器

Jump to

Keyboard shortcuts

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