Documentation ¶
Index ¶
- Constants
- Variables
- func ByteToBool(i byte) bool
- func BytesToInt(data []byte) int
- func BytesToInt16(data []byte) int16
- func BytesToInt32(data []byte) int32
- func BytesToInt64(data []byte) int64
- func BytesToUint16(data []byte) uint16
- func BytesToUint32(data []byte) uint32
- func BytesToUint64(data []byte) uint64
- func Float32bytes(float float32) []byte
- func Float64frombytes(bytes []byte) float64
- func IntToBytes(data interface{}) []byte
- func SetGlobalLogger(logger TCPLibraryLogger)
- type Conn
- type Logger
- type ServerSocket
- type Socket
- type TCPClient
- type TCPLibrary
- func (t *TCPLibrary) CloseForClientID(clientID string) error
- func (t *TCPLibrary) GetClients() *sync.Map
- func (t *TCPLibrary) NewTCPServer() *TCPServer
- func (t *TCPLibrary) SendMessageToAll(v []byte) (int, error)
- func (t *TCPLibrary) SendMessageToClients(v []byte, clientIDs ...string) (sendCount int, err error)
- func (t *TCPLibrary) SetReadBufferSize(readBufferSize int)
- func (t *TCPLibrary) SetReadDeadline(duration time.Duration)
- func (t *TCPLibrary) StopService() (err error)
- type TCPLibraryLogger
- type TCPServer
- type TCPType
Constants ¶
const ( // TCPSocketType tcp连接 TCPSocketType = iota // WebSocketType WebSocket连接 WebSocketType )
Variables ¶
var ( DefaultBufferSize = 1024 DefaultMessageChannelSize = 32 DefaultReadDeadline = 15 * time.Second )
定义的tcp读缓存区大小
Functions ¶
func ByteToBool ¶
func BytesToInt ¶
func BytesToInt16 ¶
func BytesToInt32 ¶
func BytesToInt64 ¶
func BytesToUint16 ¶
func BytesToUint32 ¶
func BytesToUint64 ¶
func Float32bytes ¶
func Float64frombytes ¶
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger 日志记录
func (*Logger) SetDefaultDebug ¶
SetDefaultDebug 设置是否为debug模式 - 只对默认日志库生效
type ServerSocket ¶
ServerSocket 服务接口,实例化tcp server时传次参数
type Socket ¶
type Socket interface { OnConnect(*Conn) error // 连接建立时 OnError(error) // 连接发生错误 OnClose(*Conn, error) // 关闭连接时 OnRecMessage(context.Context, *Conn, []byte) // 接收消息时 }
Socket tcp通讯需要的一些回调函数
type TCPClient ¶
type TCPClient struct { *TCPLibrary // contains filtered or unexported fields }
TCPClient tcp客户端
func NewTCPClient ¶
NewTCPClient 创建一个tcp客户端
func (*TCPClient) DialAndStart ¶
DialAndStart 连接到服务器,并开始读取信息
func (*TCPClient) DialAndStartTLS ¶
DialAndStartTLS 连接到服务器,并开始读取信息 tls
type TCPLibrary ¶
type TCPLibrary struct {
// contains filtered or unexported fields
}
TCPLibrary tcp库父类 - 每实例化
func NewTCPLibrary ¶
func NewTCPLibrary(debug bool, socket ServerSocket) (*TCPLibrary, error)
NewTCPLibrary 创建TCPLibrary对象 - 只用于创建服务端对象时使用,客户端直接使用 NewTCPClient
func (*TCPLibrary) CloseForClientID ¶
func (t *TCPLibrary) CloseForClientID(clientID string) error
CloseForClientID 根据clientID关闭连接
func (*TCPLibrary) GetClients ¶
func (t *TCPLibrary) GetClients() *sync.Map
GetClients 获取客户端列表,在自己的业务中使用,使用时切记小心操作
func (*TCPLibrary) NewTCPServer ¶
func (t *TCPLibrary) NewTCPServer() *TCPServer
NewTCPServer 创建一个server实例
func (*TCPLibrary) SendMessageToAll ¶
func (t *TCPLibrary) SendMessageToAll(v []byte) (int, error)
SendMessageToAll 发送给所有客户端 只有服务端可调用
func (*TCPLibrary) SendMessageToClients ¶
func (t *TCPLibrary) SendMessageToClients(v []byte, clientIDs ...string) (sendCount int, err error)
SendMessageToClients 发送数据给指定客户端 返回值,第一个值为发送成功几个连接 只有服务端可调用
func (*TCPLibrary) SetReadBufferSize ¶
func (t *TCPLibrary) SetReadBufferSize(readBufferSize int)
SetReadBufferSize 设置参数 readBufferSize
func (*TCPLibrary) SetReadDeadline ¶
func (t *TCPLibrary) SetReadDeadline(duration time.Duration)
SetReadDeadline 设置参数 readDeadline
type TCPLibraryLogger ¶
type TCPLibraryLogger interface { Infof(format string, a ...interface{}) Warnf(format string, a ...interface{}) Errorf(format string, a ...interface{}) Fatalf(format string, a ...interface{}) }
TCPLibraryLogger 日志接口,可以使用第三方日志库,符合接口即可
type TCPServer ¶
type TCPServer struct { *TCPLibrary // contains filtered or unexported fields }
TCPServer tcp服务端对象
func (*TCPServer) ListenAndServe ¶
ListenAndServe 开始tcp监听
func (*TCPServer) ListenAndServeTLS ¶
ListenAndServeTLS 开始tcp监听 tls