ws

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnNotFound 连接未找到
	ErrConnNotFound = errors.New("connection not found")
	// ErrConnNotFinish 连接未完成,不可以发送消息
	ErrConnNotFinish = errors.New("connection not finish when send msg")
)

Functions

This section is empty.

Types

type AuthHandler

type AuthHandler = func(r *http.Request, sid string, cid uint64) (uid int, ok bool)

type ConnHandlerFunc

type ConnHandlerFunc func(cid uint64, conn Connection) error

type Connection

type Connection interface {
	Start()                   //启动连接,让当前连接开始工作
	Stop()                    //停止连接,结束当前连接状态
	Context() context.Context //返回ctx,用于用户自定义的go程获取连接退出状态

	GetID() uint64        //获取当前连接ID
	GetUID() int          //获取当前连接鉴权ID
	RemoteAddr() net.Addr //获取远程客户端地址信息

	Send(ctx context.Context, mid int, data []byte) error      //发送消息
	AsyncSend(ctx context.Context, mid int, data []byte) error //异步发送消息
}

Connection 定义连接接口

func NewConnect

func NewConnect(s *wsServer, conn *websocket.Conn, id uint64, uid int) Connection

NewConnect 创建连接的方法

type Context

type Context struct {
	Req *Request
	// contains filtered or unexported fields
}

Context 上下文对象

func (*Context) Abort

func (c *Context) Abort()

Abort 退出路由

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

Deadline always returns that there is no deadline (ok==false), maybe you want to use Request.Context().Deadline() instead.

func (*Context) Done

func (c *Context) Done() <-chan struct{}

Done always returns nil (chan which will wait forever), if you want to abort your work when the connection was closed you should use Request.Context().Done() instead.

func (*Context) Err

func (c *Context) Err() error

Err always returns nil, maybe you want to use Request.Context().Err() instead.

func (*Context) Next

func (c *Context) Next()

Next 执行下一个

func (*Context) Reset

func (c *Context) Reset()

Reset 重置路由

func (*Context) Value

func (c *Context) Value(any) any

Value returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Value with the same key returns the same result.

type Engine

type Engine struct {
	RouterGroup
	// contains filtered or unexported fields
}

Engine 路由引擎

func NewEngine

func NewEngine() *Engine

NewEngine get a new engine(tcp.Handler)

func (*Engine) Start

func (e *Engine) Start(req *Request)

Start 执行入口

func (*Engine) Use

func (e *Engine) Use(middleware ...HandlerFunc)

Use set common middleware

type Handler

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

Handler 消息处理器

func NewHandler

func NewHandler(size int, e *Engine) *Handler

NewHandler 实例化消息处理器

func (*Handler) AsyncExecute

func (h *Handler) AsyncExecute(r *Request)

AsyncExecute 异步消息处理

func (*Handler) Execute

func (h *Handler) Execute(r *Request)

Execute 消息处理

func (*Handler) Init

func (h *Handler) Init(len int)

Init 初始化work池

type HandlerChain

type HandlerChain []HandlerFunc

HandlerChain 执行链

type HandlerFunc

type HandlerFunc func(c *Context)

HandlerFunc 路由处理方法

type Manager

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

Manager 连接管理模块

func NewManager

func NewManager() *Manager

NewManager 创建一个链接管理器

func (*Manager) Add

func (c *Manager) Add(conn Connection)

Add 添加连接

func (*Manager) Clear

func (c *Manager) Clear()

Clear 清除并停止所有连接

func (*Manager) Get

func (c *Manager) Get(cid uint64) (Connection, error)

Get 获取连接

func (*Manager) Len

func (c *Manager) Len() int

Len 获取连接数

func (*Manager) Range

func (c *Manager) Range(f ConnHandlerFunc) (err error)

Range 遍历所有连接

func (*Manager) Remove

func (c *Manager) Remove(conn Connection)

Remove 删除连接

type Option

type Option func(*Options)

func WithAddr

func WithAddr(addr string) Option

func WithID

func WithID(id string) Option

func WithManagerSize

func WithManagerSize(size int) Option

func WithMaxConn

func WithMaxConn(size int) Option

func WithMaxMsgChanLen

func WithMaxMsgChanLen(size int) Option

func WithMaxPacketSize

func WithMaxPacketSize(size int) Option

func WithMaxWorkerTaskLen

func WithMaxWorkerTaskLen(size int) Option

func WithName

func WithName(n string) Option

func WithOnConnAuth

func WithOnConnAuth(f AuthHandler) Option

func WithOnConnStart

func WithOnConnStart(f func(conn Connection)) Option

func WithOnConnStop

func WithOnConnStop(f func(conn Connection)) Option

func WithReadBufferSize

func WithReadBufferSize(size int) Option

func WithRouter

func WithRouter(r *Engine) Option

func WithWorkerPoolSize

func WithWorkerPoolSize(size int) Option

func WithWriteBufferSize

func WithWriteBufferSize(size int) Option

func WithWriteWait

func WithWriteWait(d time.Duration) Option

type Options

type Options struct {
	ID               string        //服务器ID
	Name             string        //服务器的名称
	Addr             string        //服务绑定的地址
	MaxPacketSize    int           //都需数据包的最大值
	MaxConn          int           //当前服务器主机允许的最大链接个数
	WorkerPoolSize   int           //业务工作Worker池的数量
	MaxWorkerTaskLen int           //业务工作Worker对应负责的任务队列最大任务存储数量
	MaxMsgChanLen    int           //SendBuffMsg发送消息的缓冲最大长度
	ManagerSize      int           //连接管理器个数
	ReadBufferSize   int           //接收缓冲区
	WriteBufferSize  int           //发送缓冲区
	WriteWait        time.Duration //写入客户端超时

	Router      *Engine               //请求路由
	OnConnStart func(conn Connection) //该Server的连接创建开始时Hook函数
	OnConnStop  func(conn Connection) //该Server的连接断开时的Hook函数
	OnConnAuth  AuthHandler           //该Server的连接鉴权完成的Hook函数
}

type Request

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

func NewRequest

func NewRequest(conn Connection, msg []byte) (*Request, error)

NewRequest 实例化请求

func (Request) Conn

func (r Request) Conn() Connection

func (Request) Data

func (r Request) Data() []byte

func (Request) Event

func (r Request) Event() string

type RouterGroup

type RouterGroup struct {
	Handlers HandlerChain
	// contains filtered or unexported fields
}

RouterGroup 路由组

func (*RouterGroup) AddRoute

func (g *RouterGroup) AddRoute(event string, handlers ...HandlerFunc)

AddRoute specific middleware

func (*RouterGroup) Use

func (g *RouterGroup) Use(middleware ...HandlerFunc)

Use 加载中间件

type Server

type Server interface {
	// Init Initialise options
	Init(...Option)
	// Options Retrieve the options
	Options() *Options
	// Start the server
	Start(ctx context.Context) error
	// Stop the server
	Stop(ctx context.Context) error
	// Endpoint return a real address to registry endpoint.
	Endpoint() (*url.URL, error)
	// GetManager 所有连接管理
	GetManager(cid uint64) *Manager
	// Range 遍历所有连接
	Range(f ConnHandlerFunc)
	// Total 服务器连接总数
	Total() int
}

Server is a simple micro server abstraction

func NewServer

func NewServer() Server

NewServer 实例化websocket服务器

Jump to

Keyboard shortcuts

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