package core import ( "context" "repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/wshandler" "github.com/go-redis/redis/v8" "go.mongodb.org/mongo-driver/bson/primitive" ) type richConnOuter struct { wsh *wshandler.WebsocketHandler rc *wshandler.Richconn } func (sub richConnOuter) JoinTag(region string, tag primitive.ObjectID, tid primitive.ObjectID, hint string) error { sub.wsh.JoinTag(region, tag, tid, sub.rc, hint) wsh := sub.wsh sub.rc.RegistOnCloseFunc(tag.Hex(), func() { wsh.LeaveTag(region, tag, tid) }) return nil } func (sub richConnOuter) LeaveTag(region string, tag primitive.ObjectID, tid primitive.ObjectID, hint string) error { sub.SetStateInTag(region, tag, tid, "", hint) return sub.wsh.LeaveTag(region, tag, tid) } func (sub richConnOuter) SetStateInTag(region string, tag primitive.ObjectID, tid primitive.ObjectID, state string, hint string) error { return sub.wsh.SetStateInTag(region, tag, tid, state, hint) } func (sub richConnOuter) TurnGroupOnline(key string, gid primitive.ObjectID, score float64) error { gidhex := gid.Hex() _, err := sub.wsh.RedisSync.ZAdd(context.Background(), key, &redis.Z{Score: score, Member: gidhex}).Result() if err != nil { logger.Error("TurnGroupOnline failed. redis.ZAdd return err :", err) return err } sub.rc.RegistOnCloseFunc(key, func() { sub.wsh.RedisSync.ZRem(context.Background(), key, gidhex) }) return nil } func (sub richConnOuter) TurnGroupOffline(key string, gid primitive.ObjectID) error { f := sub.rc.UnregistOnCloseFunc(key) if f != nil { f() } else { sub.wsh.RedisSync.ZRem(context.Background(), key, gid.Hex()) } return nil } func (sub richConnOuter) SendMessage(doc []byte) error { return sub.rc.WriteBytes(doc) } func (sub richConnOuter) SendMessageToTag(region string, tag primitive.ObjectID, msg []byte) error { sub.wsh.BroadcastRaw(region, tag, msg) return nil } func (sub richConnOuter) CloseOnPurpose() error { return sub.rc.Close() }