tok

package module
v1.0.0-...-6f3e4c5 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2016 License: MIT Imports: 13 Imported by: 0

README

tok

"talk", a library for building chat application

installation

go get github.com/quexer/tok

usage

import "github.com/quexer/tok"

TBD

cluster support

Documentation

Index

Constants

View Source
const (
	META_HEADER = "Tok-Meta"
	DV_HEADER   = "Tok-Dv"
)

Variables

View Source
var (
	READ_TIMEOUT         time.Duration = 0                //read timeout,
	WRITE_TIMEOUT                      = time.Minute      //write timeout
	AUTH_TIMEOUT                       = time.Second * 5  //auth timeout
	SERVER_PING_INTERVAL               = time.Second * 30 //server ping interval
)
View Source
var ErrOffline = errors.New("offline")
View Source
var ErrQueueRequired = errors.New("queue is required")
View Source
var (
	TCP_MAX_PACK_LEN uint32 = 4 * 1024 * 1024 //Upper limit for single message
)

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 //is invoked before OnReceive
	OnReceive(uid interface{}, data []byte)            //is invoked every time the server receive valid payload
	BeforeSend(uid interface{}, data []byte) []byte    //is invoked before Send
	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(reason string) []byte //Build payload for different reason before connection is closed
}

Application can interact with tok via this interface

type Hub

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

Dispatch message between connections

func CreateWsHandler

func CreateWsHandler(hub *Hub, config *HubConfig, txt bool) (*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 Return http handler

func Listen

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

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) Kick

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

kick all connections form uid

func (*Hub) Online

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

Query online user list

func (*Hub) Send

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

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{})

try to deliver all messages, if uid is online

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
}

Config to create new Hub

type Queue

type Queue interface {
	Enq(uid interface{}, data []byte, ttl ...uint32) error
	Deq(uid interface{}) ([]byte, error)
	Len(uid interface{}) (int, error)
}

Queue is FIFO queue interface, used by Hub

Directories

Path Synopsis
* * actor reference implementation * * queue reference implementation, using redis
* * actor reference implementation * * queue reference implementation, using redis

Jump to

Keyboard shortcuts

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