Documentation
¶
Index ¶
- Variables
- type AuthFunc
- type Client
- func (c *Client) Close()
- func (c *Client) GetMetadata(key string) (interface{}, bool)
- func (c *Client) IsInGroup(group string) bool
- func (c *Client) SendJSON(messageType string, data interface{}) error
- func (c *Client) SendMessage(msg Message) error
- func (c *Client) SetMetadata(key string, value interface{})
- type Config
- type DisconnectFunc
- type Message
- type MessageHandler
- type Middleware
- type Server
- func (s *Server) Broadcast(message Message)
- func (s *Server) BroadcastToGroup(groupName string, message Message) error
- func (s *Server) GetClient(id string) (*Client, bool)
- func (s *Server) GetClients() map[string]*Client
- func (s *Server) GetGroupCount() int
- func (s *Server) GetGroupMemberIDs(groupName string) []string
- func (s *Server) GetGroupMembers(groupName string) []*Client
- func (s *Server) GetGroups() []string
- func (s *Server) GetStats() Stats
- func (s *Server) HandleConnection(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleRaw(w http.ResponseWriter, r *http.Request) (*websocket.Conn, error)
- func (s *Server) IsGroupEmpty(groupName string) bool
- func (s *Server) JoinGroup(clientID, groupName string) error
- func (s *Server) LeaveAllGroups(clientID string)
- func (s *Server) LeaveGroup(clientID, groupName string) error
- func (s *Server) On(messageType string, handler MessageHandler)
- func (s *Server) OnConnect(fn func(*Client))
- func (s *Server) OnDisconnect(fn DisconnectFunc)
- func (s *Server) OnError(fn func(*Client, error))
- func (s *Server) SendToClient(clientID string, message Message) error
- func (s *Server) SendToGroup(groupName string, message Message) error
- func (s *Server) SendToOthersInGroup(groupName, senderID string, message Message) error
- func (s *Server) Start() error
- func (s *Server) Stop()
- func (s *Server) Use(middleware Middleware)
- type Stats
Constants ¶
This section is empty.
Variables ¶
var ( ErrServerNotRunning = errors.New("server not running") ErrClientNotFound = errors.New("client not found") ErrGroupNotFound = errors.New("group not found") ErrSendChannelFull = errors.New("send channel full") ErrConnectionLimit = errors.New("connection limit reached") ErrInvalidMessage = errors.New("invalid message") )
Common errors
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
ID string
Conn *websocket.Conn
Send chan Message
Server *Server
Groups map[string]bool
Metadata map[string]interface{}
// contains filtered or unexported fields
}
Client represents a WebSocket connection
func (*Client) GetMetadata ¶
GetMetadata returns metadata value
func (*Client) SendMessage ¶
SendMessage sends a message to the client
func (*Client) SetMetadata ¶
SetMetadata sets metadata value
type Config ¶
type Config struct {
Host string
Port int
Path string
AllowedOrigins []string
MaxConnections int
ReadBufferSize int
WriteBufferSize int
MaxMessageSize int64
PingInterval time.Duration
PongTimeout time.Duration
WriteTimeout time.Duration
}
Config holds WebSocket server configuration
type DisconnectFunc ¶
type DisconnectFunc func(client *Client)
DisconnectFunc handles client disconnection
type Message ¶
type Message struct {
Type string `json:"type"`
Data interface{} `json:"data"`
Target string `json:"target,omitempty"` // For targeted messages
From string `json:"from,omitempty"` // Client ID
}
Message represents a WebSocket message
type MessageHandler ¶
MessageHandler processes incoming messages
type Middleware ¶
type Middleware func(next MessageHandler) MessageHandler
Middleware wraps message handlers
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server manages WebSocket connections
func (*Server) BroadcastToGroup ¶
BroadcastToGroup sends a message to all clients in a group
func (*Server) GetClients ¶
GetClients returns all connected clients
func (*Server) GetGroupCount ¶
GetGroupCount returns the number of groups
func (*Server) GetGroupMemberIDs ¶
GetGroupMemberIDs returns all client IDs in a group
func (*Server) GetGroupMembers ¶
GetGroupMembers returns all clients in a group
func (*Server) HandleConnection ¶
func (s *Server) HandleConnection(w http.ResponseWriter, r *http.Request)
HandleConnection upgrades HTTP connection to WebSocket
func (*Server) HandleRaw ¶ added in v0.9.0
HandleRaw upgrades the HTTP connection to a WebSocket and returns the raw connection. Unlike HandleConnection, it does NOT register a managed Client, does NOT start readPump/writePump goroutines, and does NOT enter the message routing system. The caller owns the connection and is responsible for reading, writing, and closing it.
func (*Server) IsGroupEmpty ¶
IsGroupEmpty checks if a group has no members
func (*Server) LeaveAllGroups ¶
LeaveAllGroups removes a client from all groups
func (*Server) LeaveGroup ¶
LeaveGroup removes a client from a group
func (*Server) On ¶
func (s *Server) On(messageType string, handler MessageHandler)
On registers a message handler
func (*Server) OnDisconnect ¶
func (s *Server) OnDisconnect(fn DisconnectFunc)
OnDisconnect sets the disconnect callback
func (*Server) SendToClient ¶
SendToClient sends a message to a specific client
func (*Server) SendToGroup ¶
SendToGroup sends a message to specific clients in a group
func (*Server) SendToOthersInGroup ¶
SendToOthersInGroup sends a message to all clients in a group except the sender