Documentation
¶
Index ¶
- Variables
- func SetNoConnectionErrText(errText string)
- type AsyncTaskPool
- type ClientOption
- type Event
- type EventCallback
- type Hub
- func (h *Hub) Close()
- func (h *Hub) OnlineClientsNum() int
- func (h *Hub) PrintPushStats()
- func (h *Hub) Push(uids []string, events ...*Event) error
- func (h *Hub) PushEventHandler() func(c *gin.Context)
- func (h *Hub) PushHeartBeat(uid string)
- func (h *Hub) Serve(c *gin.Context, uid string, opts ...ServeOption)
- func (h *Hub) ServeHandler(opts ...ServeOption) func(c *gin.Context)
- type HubOption
- func WithContext(ctx context.Context, cancel context.CancelFunc) HubOption
- func WithEnableResendEvents() HubOption
- func WithLogger(logger *zap.Logger) HubOption
- func WithPushBufferSize(size int) HubOption
- func WithPushFailedHandleFn(fn func(uid string, event *Event)) HubOption
- func WithStore(store Store) HubOption
- func WithWorkerNum(num int) HubOption
- type PushRequest
- type PushStats
- type SSEClient
- type SafeMap
- func (sm *SafeMap) Clear()
- func (sm *SafeMap) Delete(uid string)
- func (sm *SafeMap) Get(uid string) (*UserClient, bool)
- func (sm *SafeMap) Has(uid string) bool
- func (sm *SafeMap) Keys() []string
- func (sm *SafeMap) Len() int
- func (sm *SafeMap) Range(f func(key, value interface{}) bool)
- func (sm *SafeMap) Set(uid string, client *UserClient)
- func (sm *SafeMap) Values() []*UserClient
- type ServeOption
- type Store
- type UserClient
- type UserEvent
Constants ¶
This section is empty.
Variables ¶
var DefaultEventType = "message"
DefaultEventType is the default event type if not provided in the event
Functions ¶
func SetNoConnectionErrText ¶
func SetNoConnectionErrText(errText string)
Types ¶
type AsyncTaskPool ¶
type AsyncTaskPool struct {
// contains filtered or unexported fields
}
func NewAsyncTaskPool ¶
func NewAsyncTaskPool(maxWorkers int) *AsyncTaskPool
NewAsyncTaskPool creates a task pool with a fixed capacity
func (*AsyncTaskPool) Stop ¶
func (p *AsyncTaskPool) Stop()
func (*AsyncTaskPool) Submit ¶
func (p *AsyncTaskPool) Submit(task func())
func (*AsyncTaskPool) Wait ¶
func (p *AsyncTaskPool) Wait()
type ClientOption ¶
type ClientOption func(*clientOptions)
func WithClientHeaders ¶
func WithClientHeaders(headers map[string]string) ClientOption
WithClientHeaders set HTTP headers
func WithClientLogger ¶
func WithClientLogger(logger *zap.Logger) ClientOption
WithClientLogger set logger
func WithClientReconnectTimeInterval ¶
func WithClientReconnectTimeInterval(d time.Duration) ClientOption
WithClientReconnectTimeInterval set reconnect time interval
type Event ¶
type Event struct { ID string `json:"id"` // event id, unique, if not provided, server will generate one Event string `json:"event"` // event type Data interface{} `json:"data"` // event data }
Event defines a Server-Sent Event
func (*Event) CheckValid ¶
CheckValid checks if the event is valid
type Hub ¶
type Hub struct { PushStats *PushStats // contains filtered or unexported fields }
Hub event center, manage client connections, receive user events, and broadcast them to online users
func (*Hub) OnlineClientsNum ¶
OnlineClientsNum get online clients num
func (*Hub) PushEventHandler ¶
PushEventHandler gin handler for push event request
func (*Hub) PushHeartBeat ¶
PushHeartBeat push heart beat event to specified user
func (*Hub) Serve ¶
func (h *Hub) Serve(c *gin.Context, uid string, opts ...ServeOption)
Serve serves a client connection
func (*Hub) ServeHandler ¶
func (h *Hub) ServeHandler(opts ...ServeOption) func(c *gin.Context)
ServeHandler gin handler for sse server
type HubOption ¶
type HubOption func(*hubOptions)
HubOption hub option
func WithContext ¶
func WithContext(ctx context.Context, cancel context.CancelFunc) HubOption
WithContext set context and cancel
func WithEnableResendEvents ¶
func WithEnableResendEvents() HubOption
WithEnableResendEvents enable resend events
func WithPushBufferSize ¶
WithPushBufferSize set push events buffer size
func WithPushFailedHandleFn ¶
WithPushFailedHandleFn set push failed handle function
type PushRequest ¶
PushRequest push request
type PushStats ¶
type PushStats struct {
// contains filtered or unexported fields
}
PushStats push stats
func (*PushStats) IncSuccess ¶
func (s *PushStats) IncSuccess()
IncSuccess increment success push count
func (*PushStats) IncTimeout ¶
func (s *PushStats) IncTimeout()
IncTimeout increment timeout push count
type SSEClient ¶
type SSEClient struct {
// contains filtered or unexported fields
}
SSEClient sse client
func NewClient ¶
func NewClient(url string, opts ...ClientOption) *SSEClient
NewClient create a new sse client
func (*SSEClient) GetConnectStatus ¶
GetConnectStatus get connect status
func (*SSEClient) OnEvent ¶
func (c *SSEClient) OnEvent(eventType string, callback EventCallback)
OnEvent register event callback
type SafeMap ¶
type SafeMap struct {
// contains filtered or unexported fields
}
SafeMap goroutine security Map structure encapsulating sync.Map
func (*SafeMap) Len ¶
Len Gets the number of elements in Map Note: Due to the nature of sync.Map, this operation is O(n) complex
func (*SafeMap) Set ¶
func (sm *SafeMap) Set(uid string, client *UserClient)
Set store key-value pairs
type ServeOption ¶
type ServeOption func(*serveOptions)
ServeOption is an option for Serve
func WithServeExtraHeaders ¶
func WithServeExtraHeaders(headers map[string]string) ServeOption
WithServeExtraHeaders sets extra headers to be sent with the response.
type Store ¶
type Store interface { Save(ctx context.Context, e *Event) error // ListByLastID list events by event type and last id, return events and last id, if no more events, return empty slice and last id "" ListByLastID(ctx context.Context, eventType string, lastID string, pageSize int) ([]*Event, string, error) }
Store defines the interface for storing and retrieving events
type UserClient ¶
type UserClient struct { UID string // user id Send chan *Event // contains filtered or unexported fields }
UserClient information