54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
|
package queue
|
||
|
|
||
|
type Client struct {
|
||
|
bro *Broker
|
||
|
}
|
||
|
|
||
|
// 设置消息容量
|
||
|
// @description 控制消息队列的大小
|
||
|
func (c *Client) SetConditions(capacity int) {
|
||
|
c.bro.setConditions(capacity)
|
||
|
}
|
||
|
|
||
|
// 消息推送
|
||
|
// @param topic 订阅的主题
|
||
|
// @param msg 传递的消息
|
||
|
func (c *Client) Publish(topic string, msg any) error {
|
||
|
return c.bro.publish(topic, msg)
|
||
|
}
|
||
|
|
||
|
// 消息订阅
|
||
|
// @description 传入订阅的主题,即可完成订阅
|
||
|
// @param topic 订阅的主题
|
||
|
// @return sub 通道用来接收数据
|
||
|
func (c *Client) Subscribe(topic string) (sub <-chan any, err error) {
|
||
|
return c.bro.subscribe(topic)
|
||
|
}
|
||
|
|
||
|
// 取消订阅
|
||
|
// @param topic 订阅的主题
|
||
|
// @param sub 消息订阅的通道
|
||
|
func (c *Client) Unsubscribe(topic string, sub <-chan any) error {
|
||
|
return c.bro.unsubscribe(topic, sub)
|
||
|
}
|
||
|
|
||
|
// 关闭消息队列
|
||
|
func (c *Client) Close() {
|
||
|
c.bro.close()
|
||
|
}
|
||
|
|
||
|
func (c *Client) GetPayload(sub <-chan any) any {
|
||
|
for val := range sub {
|
||
|
if val != nil {
|
||
|
return val
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func NewClient() *Client {
|
||
|
return &Client{
|
||
|
bro: NewBroker(),
|
||
|
}
|
||
|
}
|