connect

package
v0.0.38 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

Socket

  • 这里是对Socket的处理
  • 你完全可以定制自己的协议处理只要实现Socket接口即可

进度

  • Kcp
  • WebSocket
  • Tcp

Documentation

Overview

Package connect https://github.com/xtaci/kcp-go

Package connect https://github.com/gorilla/websocket

Index

Constants

View Source
const Addr = "0.0.0.0:12345"

Addr ----------------- 这里时测试数据

View Source
const HelloMsg = "HelloWorld"

Variables

This section is empty.

Functions

func Encode

func Encode(mp *MyProtocol) []byte

Encode 定义一个编码函数,将自定义协议的结构体转换为字节切片,用于发送数据

Types

type KcpSocket

type KcpSocket struct {
	MySocket
}

func (*KcpSocket) ListenAddr

func (socket *KcpSocket) ListenAddr(addr string)

func (*KcpSocket) ListenBack

func (socket *KcpSocket) ListenBack(f func(uuid uint32, message []byte) []byte)

type MyProtocol

type MyProtocol struct {
	Length int32  // 消息的长度,用4个字节表示
	Data   []byte // 消息的内容,用字节切片表示,长度由Length决定
}

MyProtocol 定义一个自定义协议的结构体,包含消息的长度、类型和内容

func Decode

func Decode(data []byte) *MyProtocol

Decode 定义一个解码函数,将字节切片转换为自定义协议的结构体,用于接收数据

func (*MyProtocol) SetData

func (p *MyProtocol) SetData(data []byte) *MyProtocol

type MySocket

type MySocket struct {
	UuidOnCoon sync.Map // 连接
	//queue       chan []byte                              //结果
	Pool *common.Pool //线程池

	Tls   *Tls
	Debug bool // 是否开启调试模式,开启后会打印发送日志
	// contains filtered or unexported fields
}

MySocket Socket接口的通用字段

func (*MySocket) AsyncResult

func (socket *MySocket) AsyncResult(socketId uint32, f func(bytes []byte))

AsyncResult 这里是同步的,因为流不允许并发写入

func (*MySocket) InvokeMethod

func (socket *MySocket) InvokeMethod(socketId uint32, message []byte)

InvokeMethod 此处添加至线程池进行远程调用

func (*MySocket) OnClose

func (socket *MySocket) OnClose(close func(socketId uint32))

OnClose 不允许使用此钩子注册,因为会被覆盖,请使用插件并实现SocketClose接口

func (*MySocket) SendMessage

func (socket *MySocket) SendMessage(bytes []byte)

SendMessage 广播功能

func (*MySocket) SendSelectMessage

func (socket *MySocket) SendSelectMessage(bytes []byte, socketIds ...uint32)

SendSelectMessage 选择id发送

type Socket

type Socket interface {

	// ListenBack 监听连接收到的消息,回写到上层方法,当返回 byte 不为空时则写入到客户端,
	// uuid 为连接建立时的唯一id,message为具体消息内容
	ListenBack(func(uuid uint32, message []byte) []byte)
	ListenAddr(addr string)
	SendSelectMessage(bytes []byte, socketIds ...uint32)
	SendMessage(bytes []byte)
	OnClose(close func(socketId uint32))
}

type SocketClose added in v0.0.28

type SocketClose interface {
	OnClose(socketId uint32)
}

type TcpSocket

type TcpSocket struct {
	MySocket
}

func (*TcpSocket) ListenAddr

func (socket *TcpSocket) ListenAddr(addr string)

func (*TcpSocket) ListenBack

func (socket *TcpSocket) ListenBack(f func(uuid uint32, message []byte) []byte)

type Tls

type Tls struct {
	CertFile string //证书文件
	KeyFile  string //密钥文件
}

type WebSocket

type WebSocket struct {
	MySocket
	// ws连接路径
	Path string
}

func (*WebSocket) ListenAddr

func (socket *WebSocket) ListenAddr(addr string)

func (*WebSocket) ListenBack

func (socket *WebSocket) ListenBack(f func(uuid uint32, message []byte) []byte)

Jump to

Keyboard shortcuts

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