client

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BidirectionalSession

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

BidirectionalSession 表示一个双向通信会话

func (*BidirectionalSession) Close

func (s *BidirectionalSession) Close()

Close 关闭会话

func (*BidirectionalSession) Receive

func (s *BidirectionalSession) Receive() ([]byte, bool)

Receive 接收消息(非阻塞)

func (*BidirectionalSession) ReceiveBlocking

func (s *BidirectionalSession) ReceiveBlocking() ([]byte, error)

ReceiveBlocking 接收消息(阻塞)

func (*BidirectionalSession) Send

func (s *BidirectionalSession) Send(data []byte) error

Send 发送消息到服务器

type ClientPool

type ClientPool struct {
	Addr      string      // 服务器地址
	TLSConfig *tls.Config // TLS配置
	Size      int         // 连接池目标大小

	DialTimeout time.Duration // 连接超时时间
	// contains filtered or unexported fields
}

ClientPool 管理连接池中的多个TCP连接

func New

func New(addr string, cfg *tls.Config, size int) *ClientPool

New 创建新的客户端连接池

func (*ClientPool) Call

func (p *ClientPool) Call(route string, payload []byte, timeout time.Duration) ([]byte, error)

Call 进行同步远程服务调用

func (*ClientPool) CallStream

func (p *ClientPool) CallStream(route string, payload []byte, timeout time.Duration) (<-chan []byte, error)

CallStream 进行流式远程服务调用,返回数据通道

func (*ClientPool) Close

func (p *ClientPool) Close()

Close 关闭连接池并释放所有连接资源

func (*ClientPool) CreateBidirectionalSession

func (p *ClientPool) CreateBidirectionalSession(route string, handler func([]byte)) (*BidirectionalSession, error)

CreateBidirectionalSession 创建专用的双向通信会话

func (*ClientPool) EnableBidirectional

func (p *ClientPool) EnableBidirectional()

EnableBidirectional 为连接池启用双向通信模式

func (*ClientPool) OnMessage

func (p *ClientPool) OnMessage(route string, handler func([]byte))

OnMessage 注册全局消息处理器(对所有连接生效)

func (*ClientPool) ReceiveMessage

func (p *ClientPool) ReceiveMessage() (*protocol.Frame, bool)

ReceiveMessage 接收服务器推送的消息(非阻塞,使用轮询连接)

func (*ClientPool) ReceiveMessageBlocking

func (p *ClientPool) ReceiveMessageBlocking() (*protocol.Frame, error)

ReceiveMessageBlocking 接收服务器推送的消息(阻塞,使用第一个双向连接)

func (*ClientPool) SendMessage

func (p *ClientPool) SendMessage(route string, data []byte) error

SendMessage 向服务器发送双向消息(使用第一个可用连接)

func (*ClientPool) SetCleanupInterval

func (p *ClientPool) SetCleanupInterval(interval time.Duration)

SetCleanupInterval 设置清理间隔

func (*ClientPool) SetSize

func (p *ClientPool) SetSize(size int)

SetSize 动态调整连接池大小(安全版本)

重要改进: - 不会立即关闭正在使用的连接 - 通过目标大小和清理任务逐步调整 - 只在连接空闲且满足清理条件时才关闭

type Conn

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

Conn 包装客户端连接

func (*Conn) EnableBidirectional

func (cc *Conn) EnableBidirectional()

EnableBidirectional 启用双向通信模式

func (*Conn) GetConnID

func (cc *Conn) GetConnID() uint64

GetConnID 获取当前连接ID

func (*Conn) IsDead

func (cc *Conn) IsDead() bool

func (*Conn) OnMessage

func (cc *Conn) OnMessage(route string, handler func([]byte))

OnMessage 注册消息处理器(类似WebSocket的onmessage)

func (*Conn) ReceiveMessage

func (cc *Conn) ReceiveMessage() (*protocol.Frame, bool)

ReceiveMessage 接收服务器推送的消息(非阻塞)

func (*Conn) ReceiveMessageBlocking

func (cc *Conn) ReceiveMessageBlocking() (*protocol.Frame, error)

ReceiveMessageBlocking 接收服务器推送的消息(阻塞)

func (*Conn) SendMessage

func (cc *Conn) SendMessage(route string, data []byte) error

SendMessage 主动向服务器发送消息(无需请求-响应模式)

Jump to

Keyboard shortcuts

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