You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.0 KiB
49 lines
1.0 KiB
2 months ago
|
package mq
|
||
|
|
||
|
import (
|
||
|
"github.com/go-redis/redis"
|
||
|
"wss-pool/logging/applogger"
|
||
|
)
|
||
|
|
||
|
var Rdb *redis.Client
|
||
|
|
||
|
const (
|
||
|
PublishKey = "websocket"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
Rdb = redis.NewClient(&redis.Options{
|
||
|
Addr: "localhost:6379",
|
||
|
Password: "",
|
||
|
DB: 0,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// Publish 发布消息到redis
|
||
|
// channel是发布的目标信道
|
||
|
// payload是要发布的消息内容
|
||
|
func Publish(channel string, payload string) error {
|
||
|
var err error
|
||
|
applogger.Debug("[Redis] publish [%s]: %s", channel, payload)
|
||
|
err = Rdb.Publish(channel, payload).Err()
|
||
|
if err != nil {
|
||
|
applogger.Error("[Redis] pulish error: %s", err.Error())
|
||
|
return err
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Subscribe 订阅redis消息
|
||
|
// channel是订阅的目标信道
|
||
|
func Subscribe(channel string) (string, error) {
|
||
|
applogger.Info("[Redis] subscribe [%s]", channel)
|
||
|
sub := Rdb.Subscribe(channel)
|
||
|
msg, err := sub.ReceiveMessage()
|
||
|
if err != nil {
|
||
|
applogger.Error("[Redis] subscribe [%s]", channel)
|
||
|
return "", err
|
||
|
}
|
||
|
applogger.Debug("[Redis] subscribe [%s]: %s", channel, msg.String())
|
||
|
return msg.Payload, err
|
||
|
}
|