1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package redis
- // import (
- // "context"
- // "fmt"
- // rdx "github.com/redis/go-redis/v9"
- // "sikey.com/websocket/utils/zlog"
- // )
- // type StackExchange struct {
- // channel string
- // rdb rdx.UniversalClient
- // Connect chan *RelationshipMessage
- // Disconnect chan *RelationshipMessage
- // }
- // type Config struct {
- // ServerId string
- // Cluster []string
- // Password string
- // DB int
- // }
- // func NewStackExchange(cfg Config, channel string) *StackExchange {
- // exc := &StackExchange{
- // channel: channel,
- // rdb: rdx.NewUniversalClient(&rdx.UniversalOptions{
- // Addrs: cfg.Cluster,
- // Password: cfg.Password,
- // DB: cfg.DB,
- // }),
- // }
- // go exc.run()
- // go exc.receive()
- // return exc
- // }
- // var (
- // // ws:user:clients:${uid}
- // // 存储用户和 WebSocket 连接的关系,采用有序集合方式存储
- // connectRelationship = "ws:user:clients:%s"
- // )
- // type (
- // RelationshipMessage struct {
- // ServerId string `json:"serverId"`
- // UserId string `json:"userId"`
- // }
- // )
- // // func (exc *StackExchange) Publish(msgs ...Message) {
- // // }
- // func (exc *StackExchange) OnConnect(msg RelationshipMessage) error {
- // ctx := context.Background()
- // key := fmt.Sprintf(connectRelationship, msg.UserId)
- // return exc.rdb.Set(ctx, key, msg.ServerId, 0).Err()
- // }
- // func (exc *StackExchange) OnDisconnect(msg RelationshipMessage) error {
- // ctx := context.Background()
- // key := fmt.Sprintf(connectRelationship, msg.UserId)
- // return exc.rdb.Del(ctx, key).Err()
- // }
- // func (exc *StackExchange) run() {
- // for {
- // select {
- // case msg := <-exc.Connect:
- // exc.rdb.Publish(context.Background(), exc.channel, msg)
- // case msg := <-exc.Disconnect:
- // exc.rdb.Publish(context.Background(), exc.channel, msg)
- // }
- // }
- // }
- // func (exc *StackExchange) receive() {
- // ctx, cancel := context.WithCancel(context.Background())
- // pubsub := exc.rdb.PSubscribe(ctx, exc.channel)
- // defer func() {
- // cancel()
- // pubsub.Close()
- // }()
- // for {
- // msg, err := pubsub.ReceiveMessage(ctx)
- // if err != nil {
- // zlog.Error(err)
- // cancel()
- // }
- // fmt.Println(msg)
- // }
- // }
|