event

package
v0.0.0-...-024ae4b Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: BSD-3-Clause Imports: 13 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(eventType string, ev any)

Register 注册事件

每种事件在注册时需要指定一个类型名,类型名只需要保证唯一即可

func WithChannelName

func WithChannelName(name string) func(*Options)

WithChannelName 设置通道名称

Types

type Bus

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

Bus 事件总线

func NewBus

func NewBus(queue Queue) *Bus

NewBus 构造函数

func NewNatsBus

func NewNatsBus(conn *nats.Conn, options ...func(*Options)) *Bus

NewNatsBus 使用nats构造事件总线

func NewRedisBus

func NewRedisBus(client *redis.Client, options ...func(*Options)) *Bus

NewRedisBus 构造函数

func (*Bus) Close

func (bus *Bus) Close()

Close 关闭事件总线连接

func (*Bus) Observable

func (bus *Bus) Observable() rxgo.Observable

Observable 事件观察者

func (*Bus) Publish

func (bus *Bus) Publish(ctx context.Context, event any) error

Publish 发布事件

Example:

bus.Publish(ctx, event.UserConnected{...})

func (*Bus) Subscribe

func (bus *Bus) Subscribe(ctx context.Context, handler any)

Subscribe 订阅事件

Example:

 bus.Subscribe(ctx, func(ev event.UserConnected, t time.Time) {
		// ...
 })

 bus.Subscribe(ctx, func(ev event.UserDisconnected, t time.Time) {
		// ...
 })

type Options

type Options struct {
	// ChannelName 通道名称,默认为nodehub:events
	//
	// 不同的总线实现内有不同的含义,在nats里面是topic,redis里面是channel
	ChannelName string
}

Options 配置

type Queue

type Queue = mq.Queue

Queue 消息队列

type UserConnected

type UserConnected struct {
	GatewayID  string `json:"gatewayID"`
	UserID     string `json:"userID"`
	RemoteAddr string `json:"remoteAddr"`
}

UserConnected 用户连接事件

type UserDisconnected

type UserDisconnected struct {
	GatewayID  string `json:"gatewayID"`
	UserID     string `json:"userID"`
	RemoteAddr string `json:"remoteAddr"`
}

UserDisconnected 用户断开连接事件

Jump to

Keyboard shortcuts

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