Documentation
¶
Index ¶
- type Config
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) GetRooms() []string
- func (c *Conn) ID() string
- func (c *Conn) IsClosed() bool
- func (c *Conn) IsInRoom(room string) bool
- func (c *Conn) JoinRoom(room string)
- func (c *Conn) LeaveRoom(room string)
- func (c *Conn) ReadMessage() (messageType int, data []byte, err error)
- func (c *Conn) RemoteAddr() string
- func (c *Conn) WriteJSON(v interface{}) error
- func (c *Conn) WriteMessage(messageType int, data []byte) error
- type Hub
- func (h *Hub) Broadcast(message []byte)
- func (h *Hub) BroadcastToRoom(room string, message []byte)
- func (h *Hub) GetConnectionCount() int
- func (h *Hub) GetRoomConnections(room string) int
- func (h *Hub) JoinRoom(conn *Conn, room string)
- func (h *Hub) LeaveRoom(conn *Conn, room string)
- func (h *Hub) Register(conn *Conn)
- func (h *Hub) Run(ctx context.Context)
- func (h *Hub) Unregister(conn *Conn)
- type Options
- type RoomMessage
- type Server
- type Upgrader
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 ¶
HandshakeTimeoutDuration 返回 time.Duration 类型的 HandshakeTimeout
func (*Config) PingPeriodDuration ¶
PingPeriodDuration 返回 time.Duration 类型的 PingPeriod
func (*Config) PongWaitDuration ¶
PongWaitDuration 返回 time.Duration 类型的 PongWait
func (*Config) WriteWaitDuration ¶
WriteWaitDuration 返回 time.Duration 类型的 WriteWait
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn WebSocket 连接封装
func (*Conn) ReadMessage ¶
ReadMessage 读取消息
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub WebSocket 连接中心
func (*Hub) BroadcastToRoom ¶
BroadcastToRoom 广播消息到指定房间
func (*Hub) GetRoomConnections ¶
GetRoomConnections 获取房间内的连接数
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 服务器配置选项(内部使用)
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server WebSocket 服务器
func (*Server) HandleWebSocket ¶
HandleWebSocket 处理 WebSocket 连接
Click to show internal directories.
Click to hide internal directories.