Documentation
¶
Index ¶
- Constants
- Variables
- func ExampleChatServer() *mist.HTTPServer
- func ExampleEchoServer() mist.HandleFunc
- func ExampleNotificationService() *mist.HTTPServer
- func WebSocket(config *Config, handler func(*Connection)) mist.HandleFunc
- type ChatMessage
- type Config
- type Connection
- func (c *Connection) Close() error
- func (c *Connection) Context() context.Context
- func (c *Connection) GetUserValue(key string) (interface{}, bool)
- func (c *Connection) IsConnected() bool
- func (c *Connection) Receive() (Message, error)
- func (c *Connection) Send(messageType MessageType, data []byte) error
- func (c *Connection) SendBinary(data []byte) error
- func (c *Connection) SendText(text string) error
- func (c *Connection) SetUserValue(key string, value interface{})
- type Hub
- func (h *Hub) BroadcastBinary(data []byte) int
- func (h *Hub) BroadcastBinaryToRoom(roomName string, data []byte) int
- func (h *Hub) BroadcastText(text string) int
- func (h *Hub) BroadcastTextToRoom(roomName string, text string) int
- func (h *Hub) BroadcastToAll(msgType MessageType, data []byte) int
- func (h *Hub) BroadcastToRoom(roomName string, msgType MessageType, data []byte) int
- func (h *Hub) CountConnections() int
- func (h *Hub) CountRoomConnections(roomName string) int
- func (h *Hub) GetConnection(connID string) (*Connection, bool)
- func (h *Hub) GetRoomConnections(roomName string) []string
- func (h *Hub) GetRooms() []string
- func (h *Hub) JoinRoom(roomName string, connID string) bool
- func (h *Hub) LeaveRoom(roomName string, connID string) bool
- func (h *Hub) Register(connID string, conn *Connection)
- func (h *Hub) Unregister(connID string)
- type Message
- type MessageType
- type Upgrader
Constants ¶
View Source
const ( // TextMessage 表示文本消息 TextMessage = MessageType(websocket.TextMessage) // BinaryMessage 表示二进制消息 BinaryMessage = MessageType(websocket.BinaryMessage) // CloseMessage 表示关闭连接 CloseMessage = MessageType(websocket.CloseMessage) // PingMessage 表示Ping消息 PingMessage = MessageType(websocket.PingMessage) // PongMessage 表示Pong消息 PongMessage = MessageType(websocket.PongMessage) )
Variables ¶
View Source
var ( // ErrConnectionClosed 表示WebSocket连接已关闭 ErrConnectionClosed = errors.New("websocket: 连接已关闭") // ErrMessageTooLarge 表示消息大小超过限制 ErrMessageTooLarge = errors.New("websocket: 消息太大") // ErrInvalidMessageType 表示消息类型无效 ErrInvalidMessageType = errors.New("websocket: 消息类型无效") // ErrChannelFull 表示发送通道已满 ErrChannelFull = errors.New("websocket: 发送通道已满") // ErrChannelClosed 表示通道已关闭 ErrChannelClosed = errors.New("websocket: 通道已关闭") )
Functions ¶
func ExampleNotificationService ¶
func ExampleNotificationService() *mist.HTTPServer
ExampleNotificationService 是一个实时通知服务示例
func WebSocket ¶
func WebSocket(config *Config, handler func(*Connection)) mist.HandleFunc
WebSocket 创建一个升级HTTP连接到WebSocket连接的处理函数
Types ¶
type ChatMessage ¶
type ChatMessage struct {
// 消息类型
Type string `json:"type"`
// 发送者ID
Sender string `json:"sender"`
// 目标房间
Room string `json:"room,omitempty"`
// 消息内容
Content string `json:"content"`
// 发送时间
Timestamp int64 `json:"timestamp"`
}
ChatMessage 表示聊天消息结构
type Config ¶
type Config struct {
// WriteBufferSize 是写缓冲区大小
WriteBufferSize int
// ReadBufferSize 是读缓冲区大小
ReadBufferSize int
// MaxMessageSize 是最大消息大小
MaxMessageSize int64
// HandshakeTimeout 是握手超时时间
HandshakeTimeout time.Duration
// ReadTimeout 是读取超时时间
ReadTimeout time.Duration
// WriteTimeout 是写入超时时间
WriteTimeout time.Duration
// PingInterval 是Ping间隔时间
PingInterval time.Duration
// MessageBufferSize 是消息缓冲区大小
MessageBufferSize int
// CheckOrigin 是检查Origin的函数
CheckOrigin func(r *http.Request) bool
}
Config 表示WebSocket配置
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection 表示WebSocket连接
func (*Connection) GetUserValue ¶
func (c *Connection) GetUserValue(key string) (interface{}, bool)
GetUserValue 获取用户值
func (*Connection) Send ¶
func (c *Connection) Send(messageType MessageType, data []byte) error
Send 发送消息
func (*Connection) SendBinary ¶
func (c *Connection) SendBinary(data []byte) error
SendBinary 发送二进制消息
func (*Connection) SetUserValue ¶
func (c *Connection) SetUserValue(key string, value interface{})
SetUserValue 设置用户值
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub 管理所有活跃的WebSocket连接和消息广播
func (*Hub) BroadcastBinary ¶
BroadcastBinary 向所有连接广播二进制消息
func (*Hub) BroadcastBinaryToRoom ¶
BroadcastBinaryToRoom 向房间内的所有连接广播二进制消息
func (*Hub) BroadcastTextToRoom ¶
BroadcastTextToRoom 向房间内的所有连接广播文本消息
func (*Hub) BroadcastToAll ¶
func (h *Hub) BroadcastToAll(msgType MessageType, data []byte) int
BroadcastToAll 向所有连接广播消息
func (*Hub) BroadcastToRoom ¶
func (h *Hub) BroadcastToRoom(roomName string, msgType MessageType, data []byte) int
BroadcastToRoom 向房间内的所有连接广播消息
func (*Hub) CountRoomConnections ¶
CountRoomConnections 计算房间内活跃连接数
func (*Hub) GetConnection ¶
func (h *Hub) GetConnection(connID string) (*Connection, bool)
GetConnection 获取指定ID的连接
func (*Hub) GetRoomConnections ¶
GetRoomConnections 获取房间内所有连接ID
func (*Hub) Register ¶
func (h *Hub) Register(connID string, conn *Connection)
Register 注册一个新的WebSocket连接
type Message ¶
type Message struct {
// Type 是消息类型
Type MessageType
// Data 是消息数据
Data []byte
}
Message 表示WebSocket消息
Click to show internal directories.
Click to hide internal directories.