Documentation ¶
Overview ¶
Package tok is a library to simplify creating IM application. tok supports both raw TCP and web socket connection
Index ¶
Constants ¶
const ( //META_HEADER key for meta data META_HEADER = "Tok-Meta" //DV_HEADER key for device info DV_HEADER = "Tok-Dv" )
Variables ¶
var ( //READ_TIMEOUT read timeout duration READ_TIMEOUT time.Duration //WRITE_TIMEOUT write timeout duration WRITE_TIMEOUT = time.Minute //AUTH_TIMEOUT auth timeout duration AUTH_TIMEOUT = time.Second * 5 //SERVER_PING_INTERVAL server ping interval duration SERVER_PING_INTERVAL = 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 ( //TCP_MAX_PACK_LEN upper limit for single message TCP_MAX_PACK_LEN uint32 = 4 * 1024 * 1024 )
Functions ¶
This section is empty.
Types ¶
type Actor ¶
type Actor interface { Auth(r *http.Request) (interface{}, error) //auth against http request. return uid if auth success BeforeReceive(uid interface{}, data []byte) ([]byte, error) //is invoked before OnReceive OnReceive(uid interface{}, data []byte) //is invoked every time the server receive valid payload BeforeSend(uid interface{}, data []byte) ([]byte, error) //is invoked before Send, if return value is nil, use raw data OnSent(uid interface{}, data []byte, count int) //is invoked if message is sent successfully. count mean copy quantity OnCache(uid interface{}) //is invoked after message caching //is invoked after a connection has been closed //active, count of active connections for this user OnClose(uid interface{}, active int) Ping() []byte //Build ping payload. auto ping feature will be disabled if this method return nil Bye(uid interface{}, reason string) []byte //Build payload for different reason before connection is closed }
Actor application should implement this interface to interact with tok
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub core of tok, dispatch message between connections
func CreateWsHandler ¶
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 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. return error if listen failed.
func (*Hub) CheckOnline ¶
CheckOnline return whether user online or not
func (*Hub) Send ¶
Send message to someone. ttl is expiry seconds. 0 means forever. If ttl >= 0 and user is offline, message will be cached for ttl seconds. If ttl < 0 and user is offline, ErrOffline will be returned. If ttl >=0 and user is online, but error occurred during send, message will be cached. If ttl < 0 and user is online, but error occurred during send, the error will be returned.
func (*Hub) TryDeliver ¶
func (p *Hub) TryDeliver(uid interface{})
TryDeliver try to deliver all messages, if uid is online