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.
43 lines
1.1 KiB
43 lines
1.1 KiB
|
1 year ago
|
package webstocetmsg
|
||
|
|
|
||
|
|
import (
|
||
|
|
"appPlatform/overall"
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/sirupsen/logrus"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
PublishKey = "websocket"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Publish 发布消息到redis
|
||
|
|
// channel是发布的目标信道
|
||
|
|
// payload是要发布的消息内容
|
||
|
|
func Publish(ctx context.Context, channel, payload string) (err error) {
|
||
|
|
// logrus.Debugf("[Redis] publish [%s]: %s", channel, payload)
|
||
|
|
err = overall.CONSTANT_REDIS4.Publish(ctx, channel, payload).Err()
|
||
|
|
if err != nil {
|
||
|
|
logrus.Errorf("[Redis] pulish error: %s", err.Error())
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// Subscribe 订阅redis消息
|
||
|
|
// channel是订阅的目标信道
|
||
|
|
func Subscribe(ctx context.Context, channel string) (string, error) {
|
||
|
|
// logrus.Debugf("[Redis] subscribe [%s]", channel)
|
||
|
|
sub := overall.CONSTANT_REDIS4.Subscribe(ctx, channel)
|
||
|
|
fmt.Printf("err--3->%T--2->%v\n", sub, sub)
|
||
|
|
msg, err := sub.ReceiveMessage(ctx)
|
||
|
|
fmt.Printf("err--3->%v--3->%v\n", err, msg)
|
||
|
|
if err != nil {
|
||
|
|
logrus.Errorf("[Redis] subscribe [%s]", channel)
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
// logrus.Debugf("[Redis] subscribe [%s]: %s", channel, msg.String())
|
||
|
|
return msg.Payload, err
|
||
|
|
}
|