broker

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPeerNotFound 连接未找到
	ErrPeerNotFound = errors.New("peer not found")

	// ErrRoomNotFound 房间未找到
	ErrRoomNotFound = errors.New("room not found")

	// ErrBrokerClosed 代理已关闭
	ErrBrokerClosed = errors.New("broker is closed")

	// ErrSendBufferFull 发送缓冲区已满
	ErrSendBufferFull = errors.New("send buffer full")

	// ErrInvalidPeer 无效的连接
	ErrInvalidPeer = errors.New("invalid peer: not created by this broker")

	// ErrPeerClosed 连接已关闭
	ErrPeerClosed = errors.New("peer is closed")
)

Functions

This section is empty.

Types

type Broker

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

Broker 中间人

func New

func New(handler Handler, opts ...Option) *Broker

New 创建一个新的中间人

func (*Broker) Accept

func (b *Broker) Accept(transport Transport) (Peer, error)

Accept 接受一个连接

func (*Broker) Broadcast

func (b *Broker) Broadcast(msg *Message)

Broadcast 广播消息给所有连接

func (*Broker) CreateRoom

func (b *Broker) CreateRoom() *Room

CreateRoom 创建一个房间

func (*Broker) Events

func (b *Broker) Events() *eventBus

Events 获取事件总线

func (*Broker) JoinRoom

func (b *Broker) JoinRoom(roomID string, p Peer) error

JoinRoom 加入一个房间

func (*Broker) LeaveRoom

func (b *Broker) LeaveRoom(p Peer)

LeaveRoom 离开一个房间

func (*Broker) Peer

func (b *Broker) Peer(id string) Peer

Peer 获取一个连接

func (*Broker) Peers

func (b *Broker) Peers() []Peer

Peers 获取所有连接

func (*Broker) Room

func (b *Broker) Room(id string) *Room

Room 获取一个房间

func (*Broker) Rooms

func (b *Broker) Rooms() map[string]*Room

Rooms 获取所有房间

func (*Broker) SendTo

func (b *Broker) SendTo(peerID string, msg *Message) error

SendTo 发送消息给一个连接

func (*Broker) SendToRoom

func (b *Broker) SendToRoom(roomID string, msg *Message, exclude Peer)

SendToRoom 发送消息给一个房间

func (*Broker) Start

func (b *Broker) Start(ctx context.Context)

Start 启动中间人

func (*Broker) Stop

func (b *Broker) Stop() error

Stop 停止中间人

type Event

type Event struct {
	Type EventType
	Peer Peer
	Room *Room
	Msg  *Message
}

Event 事件

type EventHandler

type EventHandler func(Event)

EventHandler 事件处理函数

type EventType

type EventType int
const (
	EventPeerConnected EventType = iota
	EventPeerDisconnected
	EventPeerJoinedRoom
	EventPeerLeftRoom
	EventMessageReceived
)

type Handler

type Handler interface {
	// HandleMessage 处理消息
	HandleMessage(from Peer, msg *Message)

	// HandleDisconnect 处理断开连接
	HandleDisconnect(from Peer)
}

Handler 处理器

type Message

type Message struct {
	Type    string         `json:"type"`
	Payload map[string]any `json:"payload"`
	To      string         `json:"to,omitempty"`
}

Message 消息

func NewMessage

func NewMessage(typ string) *Message

NewMessage 创建一个新的消息

func UnmarshalMessage

func UnmarshalMessage(data []byte) (*Message, error)

UnmarshalMessage 消息反序列化

func (*Message) Marshal

func (m *Message) Marshal() []byte

Marshal 消息序列化

type Option

type Option func(*Options)

Option 选项

func WithChannelBufSize

func WithChannelBufSize(size int) Option

WithChannelBufSize 使用自定义的通道缓冲区大小

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger 使用自定义的日志记录器

func WithMaxMessageSize

func WithMaxMessageSize(size int) Option

WithMaxMessageSize 使用自定义的最大消息大小

func WithPeerIDGenerator

func WithPeerIDGenerator(gen func() string) Option

WithPeerIDGenerator 使用自定义的连接 ID 生成器

func WithRoomAutoCleanup

func WithRoomAutoCleanup(enabled bool) Option

WithRoomAutoCleanup 使用自定义的房间自动清理设置

func WithSendBufSize

func WithSendBufSize(size int) Option

WithSendBufSize 使用自定义的发送缓冲区大小

type Options

type Options struct {
	Logger          logger.Logger
	SendBufSize     int
	ChannelBufSize  int
	MaxMessageSize  int
	PeerIDGenerator func() string
	RoomAutoCleanup bool
}

Options 代理选项

type Peer

type Peer interface {
	ID() string
	RoomID() string
	RemoteAddr() string
	Metadata() map[string]any
	SetMetadata(key string, value any)
	Send(msg *Message) error
	Close() error
}

Peer 代表一个连接

type Room

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

Room 代表一个房间

func (*Room) Add

func (r *Room) Add(p Peer)

Add 添加一个连接

func (*Room) Broadcast

func (r *Room) Broadcast(msg *Message)

Broadcast 广播消息给房间内所有连接

func (*Room) ID

func (r *Room) ID() string

ID 获取房间 ID

func (*Room) Peers

func (r *Room) Peers() []Peer

Peers 获取房间所有连接

func (*Room) Remove

func (r *Room) Remove(p Peer)

Remove 移除一个连接

func (*Room) Size

func (r *Room) Size() int

Size 获取房间连接数量

type Transport

type Transport interface {
	// ReadMessage 读取消息
	ReadMessage() ([]byte, error)

	// WriteMessage 写入消息
	WriteMessage([]byte) error

	// Close 关闭传输层
	Close() error

	// RemoteAddr 获取远程地址
	RemoteAddr() string
}

Transport 代表一个传输层

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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