Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ReadTimeout read timeout duration ReadTimeout time.Duration // WriteTimeout write timeout duration WriteTimeout = time.Minute // AuthTimeout auth timeout duration AuthTimeout = time.Second * 5 // ServerPingInterval server ping interval duration ServerPingInterval = time.Second * 30 )
var ErrOffline = errors.New("offline")
ErrOffline occurs while sending message to online user only. see Hub.Send
var ErrQueueRequired = errors.New("queue is required")
ErrQueueRequired occurs while sending "cacheable" message without queue
var ( // TCPMaxPackLen upper limit for single message TCPMaxPackLen uint32 = 4 * 1024 * 1024 )
Functions ¶
This section is empty.
Types ¶
type Actor ¶
type Actor interface { BeforeReceive(dv *Device, data []byte) ([]byte, error) // is invoked before OnReceive OnReceive(dv *Device, data []byte) // is invoked every time the server receive valid payload BeforeSend(dv *Device, data []byte) ([]byte, error) // is invoked before Send, if return value is nil, use raw data OnSent(dv *Device, data []byte) // is invoked if message is sent successfully. count mean copy quantity // OnClose is invoked after a connection has been closed // active, count of active connections for this user OnClose(dv *Device) Ping() []byte // Build ping payload. auto ping feature will be disabled if this method return nil Bye(kicker *Device, reason string, dv *Device) []byte // Build payload for different reason before connection is closed }
Actor application should implement this interface to interact with tok
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device device struct
func CreateDevice ¶
CreateDevice uid is user id, id is uuid of this device(could be empty)
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub core of tok, dispatch message between connections
func CreateWsHandler ¶
func CreateWsHandler(auth WsAuthFunc, opts ...WsHandlerOption) (*Hub, http.Handler)
CreateWsHandler create web socket http handler with hub. auth function is used for user authorization Return http handler
func Listen ¶
Listen create Tcp listener with hub. If config is not nil, a new hub will be created and replace the old one. addr is the tcp address to be listened on. auth function is used for user authorization return error if listen failed.
func (*Hub) CheckOnline ¶
CheckOnline return whether user online or not
type HubConfig ¶
type HubConfig struct { Actor Actor // Actor implement dispatch logic Q Queue // Message Queue, if nil, message to offline user will not be cached Sso bool // Default true, if it's true, new connection with same uid will kick off old ones }
HubConfig config struct for creating new Hub
func NewHubConfig ¶ added in v1.0.3
func NewHubConfig(actor Actor, opts ...HubConfigOption) *HubConfig
NewHubConfig create new HubConfig
type HubConfigOption ¶ added in v1.0.3
type HubConfigOption func(*HubConfig)
func WithHubConfigQueue ¶ added in v1.0.3
func WithHubConfigQueue(q Queue) HubConfigOption
WithHubConfigQueue set queue for hub config
func WithHubConfigSso ¶ added in v1.0.3
func WithHubConfigSso(sso bool) HubConfigOption
WithHubConfigSso set sso for hub config
type MemoryQueue ¶ added in v1.0.3
type MemoryQueue struct {
// contains filtered or unexported fields
}
func NewMemoryQueue ¶ added in v1.0.3
func NewMemoryQueue() *MemoryQueue
func (*MemoryQueue) Deq ¶ added in v1.0.3
func (mq *MemoryQueue) Deq(ctx context.Context, uid interface{}) ([]byte, error)
type Queue ¶
type Queue interface { Enq(ctx context.Context, uid interface{}, data []byte, ttl ...uint32) error Deq(ctx context.Context, uid interface{}) ([]byte, error) Len(ctx context.Context, uid interface{}) (int, error) }
Queue is FIFO queue interface, used by Hub
type TCPAuthFunc ¶
TCPAuthFunc tcp auth function parameter is the first package content of connection. return Device interface
type WsAuthFunc ¶
WsAuthFunc websocket auth function, return Device interface parameter is the initial websocket request
type WsHandler ¶ added in v1.0.3
type WsHandler struct {
// contains filtered or unexported fields
}
type WsHandlerOption ¶ added in v1.0.3
type WsHandlerOption func(*WsHandler)
func WithWsHandlerHub ¶ added in v1.0.3
func WithWsHandlerHub(hub *Hub) WsHandlerOption
WithWsHandlerHub set hub for ws handler, if hubConfig is nil, hub will be used
func WithWsHandlerHubConfig ¶ added in v1.0.3
func WithWsHandlerHubConfig(hc *HubConfig) WsHandlerOption
WithWsHandlerHubConfig set hub config for ws handler
func WithWsHandlerTxt ¶ added in v1.0.3
func WithWsHandlerTxt(txt bool) WsHandlerOption
WithWsHandlerTxt set txt for ws handler