1 回答
TA贡献1877条经验 获得超1个赞
问题与订阅频道的 *redis.PubSub 类型的对象不是用于取消订阅频道的对象有关。
因此,我必须维护对此类对象的引用,然后使用该引用取消订阅所有频道。
这是修改和工作的代码:
package main
import (
"fmt"
"github.com/go-redis/redis/v7"
"log"
)
type user struct {
name string
rooms []string
stopRunning chan bool
running bool
roomsPubsub map[string]*redis.PubSub
}
func (u *user) connect(rdb *redis.Client) error {
// get all user rooms (from DB) and start subscribe
r, err := rdb.SMembers(fmt.Sprintf("user:%s:rooms", u.name)).Result()
if err != nil {
return err
}
u.rooms = r
if len(u.rooms) == 0 {
return nil
}
u.doSubscribe("", rdb)
return nil
}
func (u *user) subscribe(room string, rdb *redis.Client) error {
// check if already subscribed
for i := range u.rooms {
if u.rooms[i] == room {
return nil
}
}
// add room to user
userRooms := fmt.Sprintf("user:%s:rooms", u.name)
if err := rdb.SAdd(userRooms, room).Err(); err != nil {
return err
}
// get all user rooms (from DB) and start subscribe
r, err := rdb.SMembers(userRooms).Result()
if err != nil {
return err
}
u.rooms = r
if u.running {
u.stopRunning <- true
}
u.doSubscribe(room, rdb)
return nil
}
func (u *user) doSubscribe(room string, rdb *redis.Client) {
pubSub := rdb.Subscribe(u.rooms...)
if len(room) > 0 {
u.roomsPubsub[room] = pubSub
}
go func() {
u.running = true
fmt.Println("starting the listener for user:", u.name, "on rooms:", u.rooms)
for {
select {
case msg, ok := <-pubSub.Channel():
if !ok {
break
}
fmt.Println(msg.Payload, msg.Channel)
case <-u.stopRunning:
fmt.Println("Stop listening for user:", u.name, "on old rooms")
for k, v := range u.roomsPubsub {
if err := v.Unsubscribe(); err != nil {
fmt.Println("unable to unsubscribe", err)
}
delete(u.roomsPubsub, k)
}
break
}
}
}()
}
func (u *user) unsubscribe(room string, rdb *redis.Client) error {
return nil
}
var rdb *redis.Client
func main() {
rdb = redis.NewClient(&redis.Options{Addr: "localhost:6379"})
u := &user{
name: "Wael",
stopRunning: make(chan bool),
roomsPubsub: make(map[string]*redis.PubSub),
}
if err := u.connect(rdb); err != nil {
log.Fatal(err)
}
if err := u.subscribe("New", rdb); err != nil {
log.Fatal(err)
}
if err := u.subscribe("Old", rdb); err != nil {
log.Fatal(err)
}
if err := u.subscribe("OldPlusPlus", rdb); err != nil {
log.Fatal(err)
}
select {}
}
- 1 回答
- 0 关注
- 136 浏览
添加回答
举报