tok

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 13 Imported by: 6

README

tok

"talk", a library to simplify creating IM application

installation

go get github.com/quexer/tok

usage

import "github/quexer/tok"

need cluster support? see https://github.com/quexer/cluster

Documentation

Index

Constants

This section is empty.

Variables

View Source
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
)
View Source
var ErrOffline = errors.New("offline")

ErrOffline occurs while sending message to online user only. see Hub.Send

View Source
var ErrQueueRequired = errors.New("queue is required")

ErrQueueRequired occurs while sending "cacheable" message without queue

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

func CreateDevice(uid interface{}, id string) *Device

CreateDevice uid is user id, id is uuid of this device(could be empty)

func (*Device) GetMeta

func (p *Device) GetMeta(key string) string

GetMeta return device meta

func (*Device) ID

func (p *Device) ID() string

ID return device uuid(could be empty)

func (*Device) PutMeta

func (p *Device) PutMeta(key string, val string)

PutMeta set device meta

func (*Device) UID

func (p *Device) UID() interface{}

UID return user id

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub core of tok, dispatch message between connections

func CreateWsHandler

func CreateWsHandler(hub *Hub, config *HubConfig, txt bool, auth WsAuthFunc) (*Hub, http.Handler)

CreateWsHandler create web socket http handler with hub. If config is not nil, a new hub will be created and replace old one If txt is true web socket will serve text frame, otherwise serve binary frame auth function is used for user authorization Return http handler

func Listen

func Listen(hub *Hub, config *HubConfig, addr string, auth TCPAuthFunc) (*Hub, error)

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

func (p *Hub) CheckOnline(uid interface{}) bool

CheckOnline return whether user online or not

func (*Hub) Kick

func (p *Hub) Kick(uid interface{})

Kick kick all connections of uid

func (*Hub) Online

func (p *Hub) Online() []interface{}

Online query online user list

func (*Hub) Send

func (p *Hub) Send(to interface{}, b []byte, ttl uint32) error

Send message to someone. ttl is expiry seconds. 0 means only send to online user If ttl = 0 and user is offline, ErrOffline will be returned. If ttl > 0 and user is offline or online but send fail, message will be cached for ttl seconds.

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  // If it's true, new connection  with same uid will kick off old ones
}

HubConfig config struct for creating new Hub

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

type TCPAuthFunc func([]byte) (*Device, error)

TCPAuthFunc tcp auth function parameter is the first package content of connection. return Device interface

type WsAuthFunc

type WsAuthFunc func(*http.Request) (*Device, error)

WsAuthFunc websocket auth function, return Device interface parameter is the initial websocket request

Jump to

Keyboard shortcuts

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