Documentation
¶
Index ¶
- Constants
- func Mount(target route.Target, hub *Hub)
- func Provider(hubs ...*Hub) provider.Provider
- type Authenticator
- type Broker
- type ChannelInfo
- type Client
- type ClientInfo
- type CloseReason
- type Config
- type Context
- func (ctx *Context) Bind(output any) error
- func (ctx *Context) Can(name string) error
- func (ctx *Context) Client() *Client
- func (ctx *Context) Context() context.Context
- func (ctx *Context) Error(code string, message string) error
- func (ctx *Context) Hub() *Hub
- func (ctx *Context) Identity() Identity
- func (ctx *Context) Message() Message
- func (ctx *Context) Publish(channel string, event string, data any) error
- func (ctx *Context) Reply(data any) error
- func (ctx *Context) Scope() *scope.Scope
- func (ctx *Context) Send(event string, data any) error
- func (ctx *Context) SetContext(value context.Context)
- func (ctx *Context) Subscribe(channel string) error
- func (ctx *Context) Unsubscribe(channel string) error
- type Filter
- type Handler
- type Hub
- func (hub *Hub) Broadcast(event string, data any) error
- func (hub *Hub) BroadcastContext(ctx context.Context, event string, data any) error
- func (hub *Hub) Channel(name string) ChannelInfo
- func (hub *Hub) Channels() []ChannelInfo
- func (hub *Hub) Check(context.Context) host.Health
- func (hub *Hub) Client(id string) (*ClientInfo, bool)
- func (hub *Hub) Clients(filters ...Filter) []ClientInfo
- func (hub *Hub) Configure(config Config) *Hub
- func (hub *Hub) HostName() string
- func (hub *Hub) Kick(clientID string, reason CloseReason) error
- func (hub *Hub) KickChannel(channel string, reason CloseReason) error
- func (hub *Hub) KickMany(clientIDs []string, reason CloseReason) error
- func (hub *Hub) Name() string
- func (hub *Hub) On(event string, handler Handler) *Hub
- func (hub *Hub) OnPublish(fn PublishHook) *Hub
- func (hub *Hub) OnSubscribe(fn SubscribeHook) *Hub
- func (hub *Hub) Publish(channel string, event string, data any) error
- func (hub *Hub) PublishContext(ctx context.Context, channel string, event string, data any) error
- func (hub *Hub) Send(clientID string, event string, data any) error
- func (hub *Hub) SendContext(ctx context.Context, clientID string, event string, data any) error
- func (hub *Hub) SendMany(clientIDs []string, event string, data any) error
- func (hub *Hub) Serve(ctx *route.Context) error
- func (hub *Hub) Start(ctx context.Context) error
- func (hub *Hub) Stats() Stats
- func (hub *Hub) Status() host.Status
- func (hub *Hub) Stop(ctx context.Context) error
- func (hub *Hub) Use(middlewares ...Middleware) *Hub
- type Identity
- type Message
- type Middleware
- type Packet
- type Presence
- type PublishHook
- type Registry
- type Response
- type Stats
- type SubscribeHook
Constants ¶
const ( EventSubscribe = "subscribe" EventUnsubscribe = "unsubscribe" EventPing = "ping" EventPong = "pong" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Authenticator ¶
Authenticator authenticates a websocket handshake.
type Broker ¶
type Broker interface {
Publish(ctx context.Context, channel string, packet Packet) error
Subscribe(ctx context.Context, node string, fn func(Packet) error) error
Close(ctx context.Context) error
}
Broker coordinates packets across nodes.
func MessageBroker ¶
MessageBroker adapts a message broker driver for distributed websocket packets.
type ChannelInfo ¶
ChannelInfo describes one channel.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is one active websocket connection.
func (*Client) Close ¶
func (client *Client) Close(reason CloseReason) error
Close closes this client.
func (*Client) Unsubscribe ¶
Unsubscribe unsubscribes this client from a channel.
type ClientInfo ¶
type ClientInfo struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Node string `json:"node,omitempty"`
Channels []string `json:"channels,omitempty"`
Connected time.Time `json:"connected"`
LastSeen time.Time `json:"last_seen"`
IP string `json:"ip,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
Meta core.Map `json:"meta,omitempty"`
}
ClientInfo describes one online client.
type Config ¶
type Config struct {
Auth Authenticator `toml:"-"`
Origin []string `toml:"origin"`
MaxMessageSize int64 `toml:"max_message_size"`
ReadTimeout time.Duration `toml:"read_timeout"`
WriteTimeout time.Duration `toml:"write_timeout"`
PingInterval time.Duration `toml:"ping_interval"`
PongTimeout time.Duration `toml:"pong_timeout"`
SendBuffer int `toml:"send_buffer"`
Broker Broker `toml:"-"`
Presence Presence `toml:"-"`
Node string `toml:"node"`
}
Config configures a websocket hub.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is one websocket message handling context.
func (*Context) SetContext ¶
SetContext replaces current message context.
func (*Context) Unsubscribe ¶
Unsubscribe unsubscribes current client from a channel.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub manages websocket clients and channels.
func (*Hub) BroadcastContext ¶
BroadcastContext sends an event to all connected clients.
func (*Hub) Channel ¶
func (hub *Hub) Channel(name string) ChannelInfo
Channel returns one channel stats.
func (*Hub) Client ¶
func (hub *Hub) Client(id string) (*ClientInfo, bool)
Client returns one client info.
func (*Hub) Clients ¶
func (hub *Hub) Clients(filters ...Filter) []ClientInfo
Clients returns online clients.
func (*Hub) Kick ¶
func (hub *Hub) Kick(clientID string, reason CloseReason) error
Kick closes one client.
func (*Hub) KickChannel ¶
func (hub *Hub) KickChannel(channel string, reason CloseReason) error
KickChannel closes all clients subscribed to a channel.
func (*Hub) KickMany ¶
func (hub *Hub) KickMany(clientIDs []string, reason CloseReason) error
KickMany closes many clients.
func (*Hub) OnPublish ¶
func (hub *Hub) OnPublish(fn PublishHook) *Hub
OnPublish registers publish hook.
func (*Hub) OnSubscribe ¶
func (hub *Hub) OnSubscribe(fn SubscribeHook) *Hub
OnSubscribe registers subscription hook.
func (*Hub) PublishContext ¶
PublishContext publishes an event to a channel.
func (*Hub) SendContext ¶
SendContext sends an event to one client.
func (*Hub) Use ¶
func (hub *Hub) Use(middlewares ...Middleware) *Hub
Use adds websocket event middleware.
type Message ¶
type Message struct {
ID string `json:"id,omitempty"`
Event string `json:"event"`
Channel string `json:"channel,omitempty"`
Data core.JSONRaw `json:"data,omitempty"`
Meta core.Map `json:"meta,omitempty"`
}
Message is a client-to-server websocket message.
type Packet ¶
type Packet struct {
Channel string `json:"channel,omitempty"`
Event string `json:"event"`
Data any `json:"data,omitempty"`
Meta core.Map `json:"meta,omitempty"`
Origin string `json:"origin,omitempty"`
}
Packet is a server push packet.
type Presence ¶
type Presence interface {
Set(ctx context.Context, client ClientInfo) error
Remove(ctx context.Context, clientID string) error
Clients(ctx context.Context, filter ...Filter) ([]ClientInfo, error)
Channels(ctx context.Context) ([]ChannelInfo, error)
Close(ctx context.Context) error
}
Presence stores online client state.
func NewMemoryPresence ¶
func NewMemoryPresence() Presence
NewMemoryPresence creates in-process presence storage.
type PublishHook ¶
PublishHook runs before publishing a packet.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry stores app-scoped websocket hubs.
type Response ¶
type Response struct {
ID string `json:"id,omitempty"`
Event string `json:"event,omitempty"`
OK bool `json:"ok"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Data any `json:"data,omitempty"`
Meta core.Map `json:"meta,omitempty"`
}
Response is a server-to-client websocket response.
type Stats ¶
type Stats struct {
Clients int `json:"clients"`
Channels int `json:"channels"`
MessagesIn uint64 `json:"messages_in"`
MessagesOut uint64 `json:"messages_out"`
BytesIn uint64 `json:"bytes_in"`
BytesOut uint64 `json:"bytes_out"`
}
Stats describes hub runtime counters.
type SubscribeHook ¶
SubscribeHook checks a subscription request.