socket_deprecated

package
v0.0.0-...-710c528 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ClientDoneCallBackFun func(clientID string)

客户端断开回调函数

View Source
var NewClientCallBackFun func(client *SClient)

新连接回调函数

Functions

This section is empty.

Types

type PackageHandler

type PackageHandler interface {
	//打包
	Package(msg []byte) []byte
	//解包
	UnPackage(msg []byte) []byte
}

数据打包处理器

type Protocol

type Protocol struct {
	Handler PackageHandler //数据编码handler
	// contains filtered or unexported fields
}

Protocol 自定义协议 解决TCP粘包问题

func NewProtocol

func NewProtocol(chanLength ...int) *Protocol

NewProtocol 初始化一个Protocol chanLength为解析成功数据channel缓冲长度

func (*Protocol) Packet

func (p *Protocol) Packet(message []byte) []byte

Packet 封包

func (*Protocol) Read

func (p *Protocol) Read() <-chan []byte

Read 获取数据读取的channel对象

func (*Protocol) Reset

func (p *Protocol) Reset()

重置

func (*Protocol) SetHeartBeat

func (p *Protocol) SetHeartBeat(b []byte)

设置心跳包数据内容,如果接收到的一条消息刚好于设置的心跳包 内容一致,这条消息将会忽略不会进入读取成功的消息队列中

func (*Protocol) Unpack

func (p *Protocol) Unpack(buffer []byte)

解析成功的数据请用Read方法获取

type SClient

type SClient struct {
	IsClose <-chan struct{} //关闭channel
	ID      string          //客户端唯一标示
	// contains filtered or unexported fields
}

TCP连接到服务器的客户端

func (*SClient) Close

func (s *SClient) Close()

关闭连接

type TcpClient

type TcpClient struct {
	IsClose <-chan struct{} //关闭channel
	// contains filtered or unexported fields
}

func NewTcpClient

func NewTcpClient(ctx context.Context, addr string) *TcpClient

新客户端对象,注意输出channel error 否则错误阻塞可能会到这无法正常运行

func (*TcpClient) Close

func (c *TcpClient) Close()

关闭连接

func (*TcpClient) Connect

func (c *TcpClient) Connect() <-chan bool

连接服务器返回连接结果是否成功

func (*TcpClient) Error

func (c *TcpClient) Error() <-chan *socket.Error

获取错误channel

func (*TcpClient) Msg

func (c *TcpClient) Msg() <-chan []byte

获取接收到的消息channel

func (*TcpClient) SetLogger

func (c *TcpClient) SetLogger(lg logs.Logger)

设置日志

func (*TcpClient) SetReConn

func (c *TcpClient) SetReConn(reconn bool, reConnectTime time.Duration)

设置是否重连

func (*TcpClient) Write

func (c *TcpClient) Write(data []byte)

发送数据

type TcpMsg

type TcpMsg struct {
	ClientID string `description:"客户端标记"`
	Data     []byte `description:"内容"`
}

数据消息

type TcpServer

type TcpServer struct {
	IsClose <-chan struct{} //关闭channel
	// contains filtered or unexported fields
}

TCP服务器对象

func NewTcpServer

func NewTcpServer(ctx context.Context, port int) *TcpServer

新建一个服务端,port为监听端口 注意处理channel error 否则程序阻塞无法正常运行

func (*TcpServer) Close

func (s *TcpServer) Close()

关闭服务

func (*TcpServer) Error

func (s *TcpServer) Error() <-chan *socket.Error

获取错误channel

func (*TcpServer) Listen

func (s *TcpServer) Listen()

启动监听该方法会阻塞

func (*TcpServer) Read

func (s *TcpServer) Read() <-chan *TcpMsg

读取消息

func (*TcpServer) SetLogger

func (s *TcpServer) SetLogger(lg logs.Logger)

设置日志

func (*TcpServer) Write

func (s *TcpServer) Write(msg *TcpMsg) error

发送数据

Jump to

Keyboard shortcuts

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