websocket

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 13 Imported by: 1

README

framework-websocket

Go framework package for websocket.

Installation

go get github.com/go-anyway/framework-websocket@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled          bool               `yaml:"enabled" env:"WEBSOCKET_ENABLED" default:"true"`
	ReadBufferSize   int                `yaml:"read_buffer_size" env:"WEBSOCKET_READ_BUFFER_SIZE" default:"4096"`
	WriteBufferSize  int                `yaml:"write_buffer_size" env:"WEBSOCKET_WRITE_BUFFER_SIZE" default:"4096"`
	HandshakeTimeout pkgConfig.Duration `yaml:"handshake_timeout" env:"WEBSOCKET_HANDSHAKE_TIMEOUT" default:"10s"`
	PingPeriod       pkgConfig.Duration `yaml:"ping_period" env:"WEBSOCKET_PING_PERIOD" default:"54s"`
	PongWait         pkgConfig.Duration `yaml:"pong_wait" env:"WEBSOCKET_PONG_WAIT" default:"60s"`
	WriteWait        pkgConfig.Duration `yaml:"write_wait" env:"WEBSOCKET_WRITE_WAIT" default:"10s"`
	MaxMessageSize   int64              `yaml:"max_message_size" env:"WEBSOCKET_MAX_MESSAGE_SIZE" default:"524288"` // 512KB
	EnableTrace      bool               `yaml:"enable_trace" env:"WEBSOCKET_ENABLE_TRACE" default:"true"`
}

Config WebSocket 配置结构体(用于从配置文件创建)

func (*Config) HandshakeTimeoutDuration

func (c *Config) HandshakeTimeoutDuration() time.Duration

HandshakeTimeoutDuration 返回 time.Duration 类型的 HandshakeTimeout

func (*Config) PingPeriodDuration

func (c *Config) PingPeriodDuration() time.Duration

PingPeriodDuration 返回 time.Duration 类型的 PingPeriod

func (*Config) PongWaitDuration

func (c *Config) PongWaitDuration() time.Duration

PongWaitDuration 返回 time.Duration 类型的 PongWait

func (*Config) ToOptions

func (c *Config) ToOptions() (*Options, error)

ToOptions 转换为 Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证 WebSocket 配置

func (*Config) WriteWaitDuration

func (c *Config) WriteWaitDuration() time.Duration

WriteWaitDuration 返回 time.Duration 类型的 WriteWait

type Conn

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

Conn WebSocket 连接封装

func (*Conn) Close

func (c *Conn) Close() error

Close 关闭连接

func (*Conn) GetRooms

func (c *Conn) GetRooms() []string

GetRooms 获取所有房间

func (*Conn) ID

func (c *Conn) ID() string

ID 返回连接 ID(用于追踪)

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed 检查连接是否已关闭

func (*Conn) IsInRoom

func (c *Conn) IsInRoom(room string) bool

IsInRoom 检查是否在指定房间

func (*Conn) JoinRoom

func (c *Conn) JoinRoom(room string)

JoinRoom 加入房间

func (*Conn) LeaveRoom

func (c *Conn) LeaveRoom(room string)

LeaveRoom 离开房间

func (*Conn) ReadMessage

func (c *Conn) ReadMessage() (messageType int, data []byte, err error)

ReadMessage 读取消息

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() string

RemoteAddr 返回远程地址

func (*Conn) WriteJSON

func (c *Conn) WriteJSON(v interface{}) error

WriteJSON 写入 JSON 消息

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType int, data []byte) error

WriteMessage 写入消息

type Hub

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

Hub WebSocket 连接中心

func NewHub

func NewHub(opts *Options) *Hub

NewHub 创建新的 Hub

func (*Hub) Broadcast

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

Broadcast 广播消息到所有连接

func (*Hub) BroadcastToRoom

func (h *Hub) BroadcastToRoom(room string, message []byte)

BroadcastToRoom 广播消息到指定房间

func (*Hub) GetConnectionCount

func (h *Hub) GetConnectionCount() int

GetConnectionCount 获取总连接数

func (*Hub) GetRoomConnections

func (h *Hub) GetRoomConnections(room string) int

GetRoomConnections 获取房间内的连接数

func (*Hub) JoinRoom

func (h *Hub) JoinRoom(conn *Conn, room string)

JoinRoom 将连接加入房间

func (*Hub) LeaveRoom

func (h *Hub) LeaveRoom(conn *Conn, room string)

LeaveRoom 将连接移出房间

func (*Hub) Register

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

Register 注册连接

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run 运行 Hub(必须在 goroutine 中运行)

func (*Hub) Unregister

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

Unregister 注销连接

type Options

type Options struct {
	ReadBufferSize   int                      // 读缓冲区大小
	WriteBufferSize  int                      // 写缓冲区大小
	HandshakeTimeout time.Duration            // 握手超时时间
	CheckOrigin      func(r interface{}) bool // 跨域检查函数(实际类型为 *http.Request,使用 interface{} 避免循环依赖)
	PingPeriod       time.Duration            // 心跳检测周期
	PongWait         time.Duration            // Pong 等待时间
	WriteWait        time.Duration            // 写等待时间
	MaxMessageSize   int64                    // 最大消息大小
	EnableTrace      bool                     // 是否启用追踪
}

Options WebSocket 服务器配置选项(内部使用)

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions 返回默认配置选项

type RoomMessage

type RoomMessage struct {
	Room    string
	Message []byte
}

RoomMessage 房间消息

type Server

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

Server WebSocket 服务器

func NewServer

func NewServer(opts *Options) *Server

NewServer 创建新的 WebSocket 服务器

func (*Server) GetHub

func (s *Server) GetHub() *Hub

GetHub 获取 Hub

func (*Server) HandleWebSocket

func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) (*Conn, error)

HandleWebSocket 处理 WebSocket 连接

func (*Server) Start

func (s *Server) Start(ctx context.Context)

Start 启动服务器(启动 Hub)

type Upgrader

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

Upgrader WebSocket 升级器

func NewUpgrader

func NewUpgrader(opts *Options) *Upgrader

NewUpgrader 创建新的 WebSocket 升级器

func (*Upgrader) Upgrade

func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error)

Upgrade 将 HTTP 连接升级为 WebSocket 连接

Jump to

Keyboard shortcuts

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