client

package
v1.7.27 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MulanPSL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 最大消息长度
	DefaultBufferSize = 512 * 1024
	// 心跳间隔
	DefaultHeartbeatInterval = 15 * time.Second
	// 读取数据超时时间
	DefaultReaderTimeOut = 60 * time.Second
)

消息及时长

Variables

This section is empty.

Functions

func GetMsgProto

func GetMsgProto(msgId string) *aiot.MsgIdProto

拼装消息ID

Types

type Client

type Client struct {
	net.Conn

	// 消息读取buffer
	Reader *bufio.Reader
	// 消息写入buffer
	Writer *bufio.Writer

	// logger
	Logger *zap.SugaredLogger
	// 守护进程间隔时间
	RestartTime time.Duration
	// 定时器
	Ticker *time.Ticker
	// contains filtered or unexported fields
}

客户端连接结构体

func NewClient

func NewClient(addr string, clientId string, deviceRegister *aiot.DeviceRegister, callBack ClientCallBack, logger *zap.SugaredLogger, restartTime time.Duration) *Client

初始化客户端

func (*Client) BusinessRequest

func (c *Client) BusinessRequest(senderId string, msgProto *aiot.MsgIdProto, data []byte) error

发送业务包请求

func (*Client) Close

func (c *Client) Close()

关闭TCP

func (*Client) GetDeviceId

func (c *Client) GetDeviceId() string

获取deviceId

func (*Client) GetState

func (c *Client) GetState() State

获取连接状态

func (*Client) InitClient

func (c *Client) InitClient()

初始化连接属性

func (*Client) IsClosed

func (c *Client) IsClosed() bool

判断连接是否关闭

func (*Client) IsConnected

func (c *Client) IsConnected() bool

连接是否在线

func (*Client) SetCallBack

func (c *Client) SetCallBack(callBack ClientCallBack)

设置回掉接口

func (*Client) SetDeviceId

func (c *Client) SetDeviceId(deviceId string)

指定节点ID

func (*Client) SetRWBuf

func (c *Client) SetRWBuf()

启用读写通道

func (*Client) SetState

func (c *Client) SetState(state State)

设置连接状态

func (*Client) StartSrv

func (c *Client) StartSrv(step time.Duration)

启动服务

func (*Client) Wait

func (c *Client) Wait()

阻塞等待

func (*Client) WriteBody

func (c *Client) WriteBody(body *aiot.Protocol) error

type ClientCallBack

type ClientCallBack interface {
	// 心跳响应
	OnHeartBeat(cli *Client, msg *aiot.Protocol)
	// 设备注册响应
	OnRegister(cli *Client, msg *aiot.Protocol) bool
	// 收到响应回调方法
	OnResponse(cli *Client, msg *aiot.Protocol)
	// 收到请求
	OnRequest(cli *Client, msg *aiot.Protocol)
	// 设备控制
	OnDeviceControl(cli *Client, msg *aiot.Protocol)
	// 数据下发
	OnDataReport(cli *Client, msg *aiot.Protocol)
	// 关闭连接回调方法
	OnClose(cli *Client)
}

回调接口

type State

type State int32

连接状态

const (
	// 初始化
	StateInit State = iota
	// 已连接
	StateConnected
	// 已注册
	StateRegistered
	// 已断开
	StateDisconnected
)

连接状态

Jump to

Keyboard shortcuts

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