air

package module
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

air

介绍

TCP框架,内部协议, 支持事件,分组,中间件

使用说明
  1. go get gitee.com/sdxstar/air

`

s, err := air.NewServer(&air.Options{
	Address: address,
})

if err != nil {
	t.Fatal(err)
}

s.On(air.OnConnect, func(ctx air.Context) error {
	t.Logf("%s connected, start to check auth", ctx.RemoteAddr())
	var reply string
	err := ctx.Ack("check_auth", nil, &reply)
	if err != nil {
		t.Error(err)
		ctx.Close()
		return nil
	}
	t.Logf("%s", reply)
	return nil
})

s.On(air.OnHeartbeat, func(ctx air.Context) error {
	return nil
})

s.On(air.OnDisconnect, func(ctx air.Context) error {
	return nil
})

s.On(air.OnClose, func(ctx air.Context) error {
	return nil
})

s.Use(middleware.Recover())

s.On("auth", func(ctx air.Context) error {
	t.Logf("%s auth", ctx.RemoteAddr())
	var auth Auth
	err := ctx.Bind(&auth)
	if err != nil {
		ctx.Close()
		return nil
	}
	ctx.Set("Hostname", auth.Hostname)
	ctx.Join("test")
	return nil
})

authGroup := s.Group(CtxAuth())

    authGroup.On("hardware", func(ctx air.Context) error {

        t.Logf("on hardware: ")
	var hardware Hardware
	if err := ctx.Bind(&hardware); err != nil {
		ctx.Close()
		return nil
	}
	conn.BroadcastTo("test", "", []byte("good"))
	return ctx.Reply("OK")
})

s.Start()

`

Documentation

Index

Constants

View Source
const (
	MaxWorkId = -1 ^ (-1 << numWorkerBits)

	MaxSequence = -1 ^ (-1 << numSequenceBits)
)

Variables

View Source
var (
	ErrConnClosed     = errors.New("use of closed network connection")
	ErrMessageTimeout = errors.New("timeout")
)

Functions

This section is empty.

Types

type Conn

type Conn interface {
	Id() string
	IsClosed() bool
	Close()
	Get(key string) interface{}
	Set(key string, value interface{})
	RemoteAddr() net.Addr

	Emit(event string, v interface{}) error
	Ack(event string, v interface{}, r interface{}) error
	BroadcastToAll(event string, msg []byte)
	BroadcastTo(room, event string, msg []byte)
	Join(room string)
	Leave(room string)
}

type Context

type Context interface {
	Id() string
	IsClosed() bool
	Close()
	Get(key string) interface{}
	Set(key string, value interface{})
	RemoteAddr() net.Addr

	Emit(event string, v interface{}) error
	Ack(event string, v interface{}, r interface{}) error
	BroadcastToAll(event string, msg []byte)
	BroadcastTo(room, event string, msg []byte)
	Join(room string)
	Leave(room string)

	Content() []byte
	Packet() protocol.Packet
	Bind(v interface{}) error

	Reply(v interface{}) error

	Conn() Conn

	Logger() logger.Logger
}

type Event

type Event = string
const (
	OnConnect    Event = "connect"
	OnJoin       Event = "join"
	OnLeave      Event = "leave"
	OnReconnect  Event = "reconnect"
	OnDisconnect Event = "disconnect"
	OnClose      Event = "close"
	OnHeartbeat  Event = "heartbeat"
)

type EventHandler

type EventHandler func(ctx Context) error

type Group

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

func NewGroup

func NewGroup(s *Server) *Group

func (*Group) Group

func (g *Group) Group(middleware ...MiddlewareFunc) (sg *Group)

func (*Group) Handler

func (g *Group) Handler(e string) EventHandler

func (*Group) On

func (g *Group) On(e string, fn EventHandler)

func (*Group) Use

func (g *Group) Use(middleware ...MiddlewareFunc)

type IdGenerator

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

func NewSnowFlake

func NewSnowFlake(workerId uint16) (*IdGenerator, error)

New returns a new snowflake node that can be used to generate snowflake

func (*IdGenerator) Generate

func (sf *IdGenerator) Generate() uint64

Next creates and returns a unique snowflake ID

type MiddlewareFunc

type MiddlewareFunc func(EventHandler) EventHandler

type Router

type Router interface {
	Use(args ...MiddlewareFunc)
	On(e string, fn EventHandler)
	Handler(e string) EventHandler
	// contains filtered or unexported methods
}

type Server

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

func NewServer

func NewServer(opts *options.ServerOptions) (srv *Server, err error)

func (*Server) BroadcastTo added in v0.4.0

func (s *Server) BroadcastTo(room, event string, msg []byte)

func (*Server) BroadcastToAll added in v0.4.0

func (s *Server) BroadcastToAll(event string, msg []byte)

func (*Server) Close

func (s *Server) Close()

func (*Server) Group

func (s *Server) Group(m ...MiddlewareFunc) (g *Group)

func (*Server) Handler

func (s *Server) Handler(e string) EventHandler

func (*Server) On

func (s *Server) On(e string, fn EventHandler)

func (*Server) SetLog

func (s *Server) SetLog(log logger.Logger)

func (*Server) SetRouter

func (e *Server) SetRouter(event string, router Router)

func (*Server) Start

func (s *Server) Start() error

Start starts service

func (*Server) Started added in v0.4.0

func (s *Server) Started() bool

func (*Server) Use

func (s *Server) Use(middleware ...MiddlewareFunc)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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