cmd

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RawMessage  = 0x01
	PingMessage = 0xf1
	PongMessage = 0xf2
)

Variables

View Source
var (
	ErrInvalidSign = errors.New("invalid sign")
)

Functions

func AddSession

func AddSession(s *Session)

func Bind

func Bind(name string, h Handler, args any, opt ...bindOptionFunc)

func BindFunc

func BindFunc(h Handler, args any, opt ...bindOptionFunc)

绑定,函数名作为消息ID 注:客户端发送的消息ID仅允许包含字母、数字

func CountSession

func CountSession() int

func Encode

func Encode(name string, i any) ([]byte, error)

func EncodePackage

func EncodePackage(pkg *Package) ([]byte, error)

func Forward

func Forward(name string, msgId string, i any)

消息通过router转发 name = "*":向所有非网关服务转发消息

func Handle

func Handle(ctx *Context, name string, data []byte) error

func Hook

func Hook(h Handler)

func ListenAndServe

func ListenAndServe(addr string) error

func RegisterService

func RegisterService(conf *ServiceConfig)

向router注册服务

func RemoveSession

func RemoveSession(id string)

func Request

func Request(serverName, msgId string, in any) ([]byte, error)

同步请求

func RequestServerAddr

func RequestServerAddr(name string) (string, error)

向路由请求服务器地址

func Route

func Route(serverId, msgId string, i any)

func RunOnce

func RunOnce()

func WithPrivate

func WithPrivate() bindOptionFunc

func WithServer

func WithServer(name string) bindOptionFunc

func WithoutQueue

func WithoutQueue() bindOptionFunc

Types

type Client

type Client struct {
	TCPConn
	// contains filtered or unexported fields
}

type CmdSet

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

func (*CmdSet) Bind

func (s *CmdSet) Bind(name string, h Handler, i any, opt ...bindOptionFunc)

func (*CmdSet) Handle

func (s *CmdSet) Handle(ctx *Context, msgId string, data []byte) error

func (*CmdSet) Hook

func (s *CmdSet) Hook(h Handler)

type Conn

type Conn interface {
	Write([]byte) error
	WriteJSON(string, any) error
	RemoteAddr() string
	Close()
}

type Context

type Context struct {
	Out         Conn   // 连接
	MsgId       string // 消息ID
	Ssid        string // 发送方会话ID
	Version     int    // 协议版本,当前未生效
	ServerName  string // 请求的协议头
	ClientAddr  string // 客户端地址
	MatchServer string // 多个服务合并后的唯一serverName
	// contains filtered or unexported fields
}

func (*Context) Fail

func (ctx *Context) Fail()

失败后不再处理后续消息

type Handler

type Handler func(*Context, any)

type M

type M map[string]any

忽略nil类型nil/slice/pointer

func (M) MarshalJSON

func (m M) MarshalJSON() ([]byte, error)

忽略空值nil

type Package

type Package struct {
	Id         string          `json:"id,omitempty"`         // 消息ID
	Data       json.RawMessage `json:"data,omitempty"`       // 数据,object类型
	Sign       string          `json:"sign,omitempty"`       // 签名
	Ssid       string          `json:"ssid,omitempty"`       // 会话ID
	Version    int             `json:"version,omitempty"`    // 版本
	Ts         int64           `json:"ts,omitempty"`         // 过期时间戳
	ServerName string          `json:"serverName,omitempty"` // 请求的协议头
	ClientAddr string          `json:"clientAddr,omitempty"` // 客户端地址

	Body any `json:"-"` // 解析成Data
}

func Decode

func Decode(buf []byte) (*Package, error)

type ServeConn

type ServeConn struct {
	*TCPConn
	// contains filtered or unexported fields
}

type Server

type Server struct {
	Addr string
}

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

type ServiceConfig

type ServiceConfig struct {
	Id        string `json:"id,omitempty"`        // 服务ID。可为空
	Name      string `json:"name,omitempty"`      // 服务名。存在多个时采用逗号,隔开
	Addr      string `json:"addr,omitempty"`      // 地址
	MinWeight int    `json:"minWeight,omitempty"` // 最小的负载
	MaxWeight int    `json:"maxWeight,omitempty"` // 最大的负载
}

type Session

type Session struct {
	Id  string
	Out Conn
}

func GetSession

func GetSession(id string) *Session

func GetSessionList

func GetSessionList() []*Session

func (*Session) Route

func (ss *Session) Route(serverId, msgId string, msgData any)

func (*Session) WriteJSON

func (ss *Session) WriteJSON(msgId string, msgData any)

type TCPConn

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

func (*TCPConn) Close

func (c *TCPConn) Close()

func (*TCPConn) ReadMessage

func (c *TCPConn) ReadMessage() (uint8, []byte, error)

func (*TCPConn) RemoteAddr

func (c *TCPConn) RemoteAddr() string

func (*TCPConn) Write

func (c *TCPConn) Write(data []byte) error

func (*TCPConn) WriteJSON

func (c *TCPConn) WriteJSON(name string, i any) error

Jump to

Keyboard shortcuts

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