socket

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Session *session

客户端管理实例

Functions

func Initialize

func Initialize(ctx context.Context, eg *errgroup.Group, fn func(name string))

func NewClient

func NewClient(conn IConn, option *ClientOption, event IEvent) error

初始化客户端信息

Types

type AckBuffer

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

AckBuffer Ack 确认缓冲区

func (*AckBuffer) Start

func (a *AckBuffer) Start(ctx context.Context) error

type AckBufferContent

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

type Channel

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

渠道管理(多渠道划分, 实现不同业务之间隔离)

func NewChannel

func NewChannel(name string, outChan chan *SenderContent) *Channel

func (*Channel) Client

func (c *Channel) Client(cid int64) (*Client, bool)

获取客户端

func (*Channel) Count

func (c *Channel) Count() int64

获取客户端连接数

func (*Channel) Name

func (c *Channel) Name() string

获取渠道名称

func (*Channel) Start

func (c *Channel) Start(ctx context.Context) error

渠道消费开启

func (*Channel) Write

func (c *Channel) Write(data *SenderContent)

推送消息到消费通道

type Client

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

WebSocket 客户端连接信息

func (*Client) Channel

func (c *Client) Channel() IChannel

func (*Client) Cid

func (c *Client) Cid() int64

获取客户端ID

func (*Client) Close

func (c *Client) Close(code int, message string)

关闭客户端连接

func (*Client) Closed

func (c *Client) Closed() bool

func (*Client) Uid

func (c *Client) Uid() int

获取客户端关联的用户ID

func (*Client) Write

func (c *Client) Write(data *ClientResponse) error

客户端写入数据

type ClientOption

type ClientOption struct {
	Uid         int         // 用户识别ID
	Channel     IChannel    // 渠道信息
	Storage     IStorage    // 自定义缓存组件, 用于绑定用户与客户端的关系
	IdGenerator IdGenerator // 客户端ID生成器(唯一ID), 默认使用雪花算法
	Buffer      int         // 缓冲区大小根据业务, 自行调整
}

type ClientResponse

type ClientResponse struct {
	IsAck   bool   `json:"-"`                 // 是否需要 ack 回调
	Sid     string `json:"sid,omitempty"`     // ACK ID
	Event   string `json:"event"`             // 事件名
	Content any    `json:"content,omitempty"` // 事件内容
	Retry   int    `json:"-"`                 // 重试次数(0 默认不重试)
}

type CloseEvent

type CloseEvent func(client IClient, code int, text string)

type DestroyEvent

type DestroyEvent func(client IClient)

type Event

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

func (*Event) Close

func (c *Event) Close(client IClient, code int, text string)

func (*Event) Destroy

func (c *Event) Destroy(client IClient)

func (*Event) Message

func (c *Event) Message(client IClient, data []byte)

func (*Event) Open

func (c *Event) Open(client IClient)

type EventOption

type EventOption func(event *Event)

func WithCloseEvent

func WithCloseEvent(e CloseEvent) EventOption

WithCloseEvent 连接关闭回调事件

func WithDestroyEvent

func WithDestroyEvent(e DestroyEvent) EventOption

WithDestroyEvent 连接销毁回调事件

func WithMessageEvent

func WithMessageEvent(e MessageEvent) EventOption

WithMessageEvent 消息回调事件

func WithOpenEvent

func WithOpenEvent(e OpenEvent) EventOption

WithOpenEvent 连接成功回调事件

type IChannel

type IChannel interface {
	Name() string
	Count() int64
	Client(cid int64) (*Client, bool)
	Write(data *SenderContent)
	// contains filtered or unexported methods
}

type IClient

type IClient interface {
	Cid() int64                       // 客户端ID
	Uid() int                         // 客户端关联用户ID
	Close(code int, text string)      // 关闭客户端
	Write(data *ClientResponse) error // 写入数据
	Channel() IChannel                // 获取客户端所属渠道
}

type IConn

type IConn interface {
	// Read 数据读取
	Read() ([]byte, error)
	// Write 数据写入
	Write([]byte) error
	// Close 连接关闭
	Close() error
	// SetCloseHandler 设置连接关闭回调事件
	SetCloseHandler(fn func(code int, text string) error)
	// Network 网络协议类型
	Network() string
}

type IEvent

type IEvent interface {
	Open(client IClient)
	Message(client IClient, data []byte)
	Close(client IClient, code int, text string)
	Destroy(client IClient)
}

func NewEvent

func NewEvent(opts ...EventOption) IEvent

type IStorage

type IStorage interface {
	Bind(ctx context.Context, channel string, cid int64, uid int) error
	UnBind(ctx context.Context, channel string, cid int64) error
}

type IdGenerator

type IdGenerator interface {
	//   获取自增ID
	IdGen() int64
}

type Message

type Message struct {
	Event   string `json:"event"`   // 事件名称
	Content any    `json:"content"` // 消息内容
}

客户端交互的消息体

func NewMessage

func NewMessage(event string, content any) *Message

type MessageEvent

type MessageEvent func(client IClient, data []byte)

type OpenEvent

type OpenEvent func(client IClient)

type SenderContent

type SenderContent struct {
	IsAck bool
	// contains filtered or unexported fields
}

推送的消息

func NewSenderContent

func NewSenderContent() *SenderContent

func (*SenderContent) IsBroadcast

func (s *SenderContent) IsBroadcast() bool

判断是否是广播推送

func (*SenderContent) SetAck

func (s *SenderContent) SetAck(value bool) *SenderContent

func (*SenderContent) SetBroadcast

func (s *SenderContent) SetBroadcast(value bool) *SenderContent

设置广播推送

func (*SenderContent) SetExclude

func (s *SenderContent) SetExclude(cid ...int64) *SenderContent

设置广播推送中需要过滤的客户端

func (*SenderContent) SetMessage

func (s *SenderContent) SetMessage(event string, content any) *SenderContent

func (*SenderContent) SetReceive

func (s *SenderContent) SetReceive(cid ...int64) *SenderContent

设置推送客户端

type SnowflakeGenerator

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

func (*SnowflakeGenerator) IdGen

func (s *SnowflakeGenerator) IdGen() int64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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