websocket

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 AuthFunc

type AuthFunc func(client *Client, data interface{}) error

AuthFunc handles client authentication

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) Close

func (c *Client) Close()

Close closes the client connection

func (*Client) GetMetadata

func (c *Client) GetMetadata(key string) (interface{}, bool)

GetMetadata returns metadata value

func (*Client) IsInGroup

func (c *Client) IsInGroup(group string) bool

IsInGroup checks if client is in a group

func (*Client) SendJSON

func (c *Client) SendJSON(messageType string, data interface{}) error

SendJSON sends a JSON message to the client

func (*Client) SendMessage

func (c *Client) SendMessage(msg Message) error

SendMessage sends a message to the client

func (*Client) SetMetadata

func (c *Client) SetMetadata(key string, value interface{})

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

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default 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

type MessageHandler func(client *Client, message Message) error

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 New

func New(config Config) *Server

New creates a new WebSocket server

func (*Server) Broadcast

func (s *Server) Broadcast(message Message)

Broadcast sends a message to all connected clients

func (*Server) BroadcastToGroup

func (s *Server) BroadcastToGroup(groupName string, message Message) error

BroadcastToGroup sends a message to all clients in a group

func (*Server) GetClient

func (s *Server) GetClient(id string) (*Client, bool)

GetClient returns a client by ID

func (*Server) GetClients

func (s *Server) GetClients() map[string]*Client

GetClients returns all connected clients

func (*Server) GetGroupCount

func (s *Server) GetGroupCount() int

GetGroupCount returns the number of groups

func (*Server) GetGroupMemberIDs

func (s *Server) GetGroupMemberIDs(groupName string) []string

GetGroupMemberIDs returns all client IDs in a group

func (*Server) GetGroupMembers

func (s *Server) GetGroupMembers(groupName string) []*Client

GetGroupMembers returns all clients in a group

func (*Server) GetGroups

func (s *Server) GetGroups() []string

GetGroups returns all group names

func (*Server) GetStats

func (s *Server) GetStats() Stats

GetStats returns server statistics

func (*Server) HandleConnection

func (s *Server) HandleConnection(w http.ResponseWriter, r *http.Request)

HandleConnection upgrades HTTP connection to WebSocket

func (*Server) IsGroupEmpty

func (s *Server) IsGroupEmpty(groupName string) bool

IsGroupEmpty checks if a group has no members

func (*Server) JoinGroup

func (s *Server) JoinGroup(clientID, groupName string) error

JoinGroup adds a client to a group

func (*Server) LeaveAllGroups

func (s *Server) LeaveAllGroups(clientID string)

LeaveAllGroups removes a client from all groups

func (*Server) LeaveGroup

func (s *Server) LeaveGroup(clientID, groupName string) error

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) OnConnect

func (s *Server) OnConnect(fn func(*Client))

OnConnect sets the connect callback

func (*Server) OnDisconnect

func (s *Server) OnDisconnect(fn DisconnectFunc)

OnDisconnect sets the disconnect callback

func (*Server) OnError

func (s *Server) OnError(fn func(*Client, error))

OnError sets the error callback

func (*Server) SendToClient

func (s *Server) SendToClient(clientID string, message Message) error

SendToClient sends a message to a specific client

func (*Server) SendToGroup

func (s *Server) SendToGroup(groupName string, message Message) error

SendToGroup sends a message to specific clients in a group

func (*Server) SendToOthersInGroup

func (s *Server) SendToOthersInGroup(groupName, senderID string, message Message) error

SendToOthersInGroup sends a message to all clients in a group except the sender

func (*Server) Start

func (s *Server) Start() error

Start begins processing WebSocket connections

func (*Server) Stop

func (s *Server) Stop()

Stop gracefully shuts down the server

func (*Server) Use

func (s *Server) Use(middleware Middleware)

Use adds middleware

type Stats

type Stats struct {
	ConnectedClients int64
	MessagesSent     int64
	MessagesReceived int64
	BytesSent        int64
	BytesReceived    int64
}

Stats holds server statistics

Jump to

Keyboard shortcuts

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