UpdateChannelDocument 제거
This commit is contained in:
@ -6,8 +6,10 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
@ -20,9 +22,16 @@ func init() {
|
||||
}
|
||||
|
||||
type channelID = string
|
||||
type channelConfig struct {
|
||||
Capacity int64 `json:"capacity"`
|
||||
Size int64 `json:"size"`
|
||||
Key string `json:"key"`
|
||||
emptyJson string
|
||||
}
|
||||
|
||||
type chatConfig struct {
|
||||
DefaultCapacity int64 `json:"default_capacity"`
|
||||
Channels map[string]map[string]any `json:"channels"`
|
||||
DefaultCapacity int64 `json:"default_capacity"`
|
||||
Channels map[string]channelConfig `json:"channels"`
|
||||
}
|
||||
|
||||
type groupChat struct {
|
||||
@ -58,15 +67,15 @@ func (gc *groupChat) Initialize(sub *subTavern, cfg configDocument) error {
|
||||
sub.apiFuncs.registApiFunction("BroadcastMessageOnChannel", gc.BroadcastMessageOnChannel)
|
||||
sub.apiFuncs.registApiFunction("QueryPlayerChattingChannel", gc.QueryPlayerChattingChannel)
|
||||
sub.apiFuncs.registApiFunction("SendMessageOnChannel", gc.SendMessageOnChannel)
|
||||
sub.apiFuncs.registApiFunction("UpdateChannelDocument", gc.UpdateChannelDocument)
|
||||
for name, cfg := range gc.chatConfig.Channels {
|
||||
if _, ok := cfg["capacity"]; !ok {
|
||||
cfg["capacity"] = gc.chatConfig.DefaultCapacity
|
||||
} else {
|
||||
cfg["capacity"] = int64(cfg["capacity"].(float64))
|
||||
if cfg.Capacity == 0 {
|
||||
cfg.Capacity = gc.chatConfig.DefaultCapacity
|
||||
}
|
||||
cfg["key"] = name
|
||||
cfg["size"] = int32(0)
|
||||
cfg.Key = name
|
||||
cfg.Size = 0
|
||||
|
||||
jm, _ := json.Marshal(cfg)
|
||||
cfg.emptyJson = fmt.Sprintf("[%s]", string(jm))
|
||||
|
||||
_, err := gc.rh.JSONSet(name, "$", cfg)
|
||||
if *devflag && err != nil {
|
||||
@ -100,27 +109,9 @@ func (gc *groupChat) Initialize(sub *subTavern, cfg configDocument) error {
|
||||
|
||||
func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandler.WebSocketMessageType, message any) {
|
||||
if mt == wshandler.Disconnected {
|
||||
rooms := message.([]string)
|
||||
for _, chanid := range rooms {
|
||||
if _, ok := gc.chatConfig.Channels[chanid]; !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
size, err := gc.rh.JSONNumIncrBy(chanid, "$.size", -1)
|
||||
if err != nil {
|
||||
logger.Println("JSONMGet failed :", err)
|
||||
continue
|
||||
}
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"size": size},
|
||||
Tag: []string{"ChattingChannelProperties"},
|
||||
})
|
||||
}
|
||||
if _, err := gc.rh.Del(gc.rh.Context(), accidHex(sender.Accid)).Result(); err != nil {
|
||||
logger.Println(err)
|
||||
}
|
||||
|
||||
} else if mt == wshandler.BinaryMessage {
|
||||
commandline := message.([]any)
|
||||
cmd := commandline[0].(string)
|
||||
@ -132,11 +123,22 @@ func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandle
|
||||
size, err := gc.rh.JSONGetInt64(chanid, "$.size")
|
||||
if err != nil || len(size) == 0 {
|
||||
logger.Println("JSONGetInt64 failed :", chanid, err)
|
||||
} else if size[0] < cfg["capacity"].(int64) {
|
||||
} else if size[0] < cfg.Capacity {
|
||||
// 입장
|
||||
newsize, err := gc.rh.JSONNumIncrBy(chanid, "$.size", 1)
|
||||
if err == nil {
|
||||
gc.enterRoom(chanid, sender.Accid)
|
||||
sender.RegistDisconnectedCallback(chanid, func() {
|
||||
size, err := gc.rh.JSONNumIncrBy(chanid, "$.size", -1)
|
||||
if err == nil {
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"size": size},
|
||||
Tag: []string{"ChattingChannelProperties"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
gc.rh.HSet(gc.rh.Context(), accidHex(sender.Accid), "cc_pub", chanid)
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
@ -156,13 +158,8 @@ func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandle
|
||||
chanid := args[0].(string)
|
||||
gc.rh.HDel(gc.rh.Context(), accidHex(sender.Accid), "cc_pub")
|
||||
gc.leaveRoom(chanid, sender.Accid)
|
||||
newsize, err := gc.rh.JSONNumIncrBy(chanid, "$.size", 1)
|
||||
if err == nil {
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"size": newsize[0]},
|
||||
Tag: []string{"ChattingChannelProperties"},
|
||||
})
|
||||
if f := sender.PopDisconnectedCallback(chanid); f != nil {
|
||||
f()
|
||||
}
|
||||
|
||||
case "TextMessage":
|
||||
@ -191,6 +188,18 @@ func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandle
|
||||
return
|
||||
}
|
||||
gc.enterRoom(channel, sender.Accid)
|
||||
|
||||
sender.RegistDisconnectedCallback(channel, func() {
|
||||
gc.rh.JSONDel(channel, "$."+sender.Accid.Hex())
|
||||
cnt, _ := gc.rh.HDel(gc.rh.Context(), accidHex(sender.Accid), "cc_"+typename).Result()
|
||||
if cnt > 0 {
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + channel,
|
||||
Body: map[string]any{"sender": sender.Alias, "typename": typename},
|
||||
Tag: []string{"LeavePrivateChannel"},
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
@ -204,17 +213,11 @@ func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandle
|
||||
})
|
||||
|
||||
case "LeavePrivateChannel":
|
||||
typename := args[0].(string)
|
||||
channel := args[1].(string)
|
||||
cnt, _ := gc.rh.HDel(gc.rh.Context(), accidHex(sender.Accid), "cc_"+typename).Result()
|
||||
if cnt > 0 {
|
||||
gc.leaveRoom(channel, sender.Accid)
|
||||
gc.leaveRoom(channel, sender.Accid)
|
||||
if f := sender.PopDisconnectedCallback(channel); f != nil {
|
||||
f()
|
||||
}
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + channel,
|
||||
Body: map[string]any{"sender": sender.Alias, "typename": typename},
|
||||
Tag: []string{"LeavePrivateChannel"},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -227,23 +230,26 @@ func (gc *groupChat) FetchChattingChannels(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
keys, err := gc.rh.Keys(gc.rh.Context(), prefix+"*").Result()
|
||||
if err != nil {
|
||||
logger.Println("FetchChattingChannel failed. Keys return err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var rows []string
|
||||
for _, key := range keys {
|
||||
onechan, err := gc.rh.JSONGet(key, "$")
|
||||
if err != nil {
|
||||
for name, cfg := range gc.chatConfig.Channels {
|
||||
if len(prefix) > 0 {
|
||||
if !strings.HasPrefix(name, prefix) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
onechan, err := gc.rh.JSONGet(name, "$")
|
||||
if err != nil && err != redis.Nil {
|
||||
logger.Println("FetchChattingChannel failed. HGetAll return err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
row := onechan.(string)
|
||||
rows = append(rows, row)
|
||||
|
||||
if err == redis.Nil || onechan == nil {
|
||||
rows = append(rows, cfg.emptyJson)
|
||||
} else {
|
||||
rows = append(rows, onechan.(string))
|
||||
}
|
||||
}
|
||||
|
||||
if len(rows) == 0 {
|
||||
@ -291,58 +297,6 @@ func (gc *groupChat) QueryPlayerChattingChannel(w http.ResponseWriter, r *http.R
|
||||
enc.Encode(output)
|
||||
}
|
||||
|
||||
func (gc *groupChat) UpdateChannelDocument(w http.ResponseWriter, r *http.Request) {
|
||||
channel, _ := gocommon.ReadStringFormValue(r.Form, "channel")
|
||||
key, _ := gocommon.ReadStringFormValue(r.Form, "key")
|
||||
ret, _ := gocommon.ReadStringFormValue(r.Form, "return")
|
||||
if len(channel) == 0 || len(key) == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
bt, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var doc map[string]any
|
||||
if err := bson.Unmarshal(bt, &doc); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if ret == "before" {
|
||||
before, err2 := gc.rh.JSONGet(channel, "$")
|
||||
if err2 != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err == nil {
|
||||
w.Write([]byte(before.(string)))
|
||||
}
|
||||
}()
|
||||
} else if ret == "after" {
|
||||
defer func() {
|
||||
if err == nil {
|
||||
after, err2 := gc.rh.JSONGet(channel, "$")
|
||||
if err2 != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
} else {
|
||||
w.Write([]byte(after.(string)))
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
_, err = gc.rh.JSONSet(channel, "$."+key, doc)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (gc *groupChat) SendMessageOnChannel(w http.ResponseWriter, r *http.Request) {
|
||||
channel, _ := gocommon.ReadStringFormValue(r.Form, "channel")
|
||||
target, _ := gocommon.ReadStringFormValue(r.Form, "target")
|
||||
|
||||
@ -245,28 +245,24 @@ func (sub *subTavern) OnClientMessageReceived(sender *wshandler.Sender, messageT
|
||||
}
|
||||
|
||||
func (sub *subTavern) OnRoomCreated(region, name string) {
|
||||
created, err := sub.redison.JSONSet(name, "$", map[string]any{
|
||||
"_refcnt": 1,
|
||||
}, gocommon.RedisonSetOptionNX)
|
||||
cnt, err := sub.redison.IncrBy(sub.redison.Context(), "_ref_"+name, 1).Result()
|
||||
if err != nil && !errors.Is(err, redis.Nil) {
|
||||
logger.Println("OnRoomCreated JSONSet failed :", err)
|
||||
return
|
||||
}
|
||||
if !created {
|
||||
_, err = sub.redison.JSONNumIncrBy(name, "$._refcnt", 1)
|
||||
if err != nil {
|
||||
logger.Println("OnRoomCreated JSONSet failed :", err)
|
||||
return
|
||||
}
|
||||
|
||||
if cnt == 1 {
|
||||
sub.redison.JSONSet(name, "$", map[string]any{})
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) OnRoomDestroyed(region, name string) {
|
||||
cnt, err := sub.redison.JSONNumIncrBy(name, "$._refcnt", -1)
|
||||
if err != nil || len(cnt) == 0 {
|
||||
cnt, err := sub.redison.IncrBy(sub.redison.Context(), "_ref_"+name, -1).Result()
|
||||
if err != nil {
|
||||
logger.Println("OnRoomDestroyed JSONNumIncrBy failed :", err)
|
||||
} else if cnt[0] == 0 {
|
||||
sub.redison.Del(sub.redison.Context(), name)
|
||||
} else if cnt == 0 {
|
||||
sub.redison.Del(sub.redison.Context(), "_ref_"+name)
|
||||
sub.redison.JSONDel(name, "$")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user