socket

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: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

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

func NewError

func NewError(t ErrorType, err error) *Error

新增错误

func (*Error) Error

func (e *Error) Error() error

错误信息

func (*Error) T

func (e *Error) T() ErrorType

错误类型

type ErrorType

type ErrorType int

错误类型

const (
	ConnectErr ErrorType = iota + 1 //连接错误
	ReadErr                         //消息读取错误
	SendErr                         //消息发送错误
	ListenErr                       //服务器监听错误
	CancelErr                       //关闭[废弃]
)

func (ErrorType) String

func (e ErrorType) String() string

type Protocol

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

Protocol 自定义协议解析

func NewProtocol

func NewProtocol(chanLength ...int) *Protocol

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

func (*Protocol) Packing

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

Packet 封包

func (*Protocol) ReadMsg

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

Read 获取数据读取的channel对象

func (*Protocol) Reset

func (p *Protocol) Reset()

重置

func (*Protocol) UnPacking

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

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

type SClient

type SClient struct {
	ID string //客户端唯一标示
	// contains filtered or unexported fields
}

TCP连接到服务器的客户端

func (*SClient) Close

func (s *SClient) Close()

关闭连接

type TcpClient

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

tcp客户端

func NewTcpClient

func NewTcpClient(ctx context.Context, config *TcpClientConfig) (*TcpClient, error)

创建Tcp客户端

func (*TcpClient) Close

func (c *TcpClient) Close()

关闭连接

func (*TcpClient) Connect

func (c *TcpClient) Connect() bool

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

func (*TcpClient) ConnectAsync

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

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

func (*TcpClient) Write

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

发送数据

type TcpClientConfig

type TcpClientConfig struct {
	ServerAddress     string                 //服务地址
	AutoReConnect     bool                   //自动重连
	ReConnectWaitTime time.Duration          //重连等待时间
	Protocol          TcpProtocol            //连接处理协议,如果没有则收到什么数据返回什么数据,写入什么数据发送什么数据
	Log               logs.Logger            //日志
	ErrorHandler      func(ErrorType, error) //错误处理
	ConnectHandler    func()                 //连接成功调用
	MessageHandler    func([]byte)           //消息处理
	CloseHandler      func()                 //连接关闭处理
	HeartBeatData     []byte                 //心跳内容,如果为空不发送心跳信息
	HeartBeatTime     time.Duration          //心跳间隔
	ConnectTimeOut    time.Duration          //连接超时事件
}

tcp客户端配置

type TcpProtocol

type TcpProtocol interface {
	Packing(data []byte) []byte //打包数据
	UnPacking(data []byte)      //解析数据
	ReadMsg() <-chan []byte     //读取解析出来的有效数据
}

tcp连接协议

type TcpServer

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

TCP服务器对象

func NewTcpServer

func NewTcpServer(ctx context.Context, config *TcpServerConfig) (*TcpServer, error)

新建一个服务端

func (*TcpServer) Close

func (s *TcpServer) Close()

关闭服务

func (*TcpServer) CloseClient

func (s *TcpServer) CloseClient(clientID string)

关闭指定客户端

func (*TcpServer) Listen

func (s *TcpServer) Listen()

启动监听该方法会

func (*TcpServer) Write

func (s *TcpServer) Write(clientID string, msg []byte) error

发送数据

type TcpServerConfig

type TcpServerConfig struct {
	Port               int64                                                  //监听端口
	ServerAddress      string                                                 //服务监听地址
	Log                logs.Logger                                            //日志
	ErrorHandler       func(errType ErrorType, err error, clientID ...string) //错误处理
	NewClientHandler   func(clientID string) TcpProtocol                      //新客户端连接回调,返回该客户端处理协议,可以返回nil
	MessageHandler     func(clientID string, msg []byte)                      //消息处理
	CloseHandler       func()                                                 //服务关闭回调
	ClientCloseHandler func(clientID string)                                  //客户端关闭回调
}

tcp服务端配置

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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