WebsocketApiHandler로 변경
This commit is contained in:
@ -5,10 +5,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
@ -35,9 +33,6 @@ type groupChat struct {
|
||||
sendUpstreamMessage func(msg *wshandler.UpstreamMessage)
|
||||
}
|
||||
|
||||
var accidHex func(primitive.ObjectID) string
|
||||
var accidstrHex func(string) string
|
||||
|
||||
func (gc *groupChat) Initialize(tv *Tavern, cfg configDocument) error {
|
||||
rem, _ := json.Marshal(cfg)
|
||||
if err := json.Unmarshal(rem, &gc.chatConfig); err != nil {
|
||||
@ -68,7 +63,7 @@ func (gc *groupChat) Initialize(tv *Tavern, cfg configDocument) error {
|
||||
|
||||
_, err := gc.rh.JSONSet(name, "$", cfg)
|
||||
if *devflag && err != nil {
|
||||
gc.rh.Del(gc.rh.Context(), name).Result()
|
||||
gc.rh.JSONDel(name, "$")
|
||||
_, err = gc.rh.JSONSet(name, "$", cfg)
|
||||
}
|
||||
|
||||
@ -77,140 +72,245 @@ func (gc *groupChat) Initialize(tv *Tavern, cfg configDocument) error {
|
||||
}
|
||||
}
|
||||
|
||||
ts := fmt.Sprintf("%x-", time.Now().Unix())
|
||||
if *devflag {
|
||||
accidHex = func(accid primitive.ObjectID) string {
|
||||
return ts + accid.Hex()
|
||||
}
|
||||
accidstrHex = func(accid string) string {
|
||||
return ts + accid
|
||||
}
|
||||
} else {
|
||||
accidHex = func(accid primitive.ObjectID) string {
|
||||
return accid.Hex()
|
||||
}
|
||||
accidstrHex = func(accid string) string {
|
||||
return accid
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandler.WebSocketMessageType, message any) {
|
||||
if mt == wshandler.Disconnected {
|
||||
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)
|
||||
args := commandline[1:]
|
||||
switch cmd {
|
||||
case "EnterPublicChannel":
|
||||
chanid := args[0].(string)
|
||||
if cfg, ok := gc.chatConfig.Channels[chanid]; ok {
|
||||
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 {
|
||||
// 입장
|
||||
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"},
|
||||
})
|
||||
}
|
||||
})
|
||||
func (gc *groupChat) ClientConnected(ctx wshandler.ApiCallContext) {
|
||||
gc.rh.JSONSet(ctx.CallBy.Accid.Hex(), "$.channel", map[string]any{})
|
||||
}
|
||||
|
||||
gc.rh.HSet(gc.rh.Context(), accidHex(sender.Accid), "cc_pub", chanid)
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"size": newsize[0]},
|
||||
Tag: []string{"ChattingChannelProperties"},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// 풀방
|
||||
logger.Println("chatting channel is full :", chanid, size, cfg.Capacity)
|
||||
}
|
||||
func (gc *groupChat) ClientDisconnected(ctx wshandler.ApiCallContext) {
|
||||
docs, _ := gc.rh.JSONGetDocuments(ctx.CallBy.Accid.Hex(), "$.channel")
|
||||
|
||||
if len(docs) > 0 {
|
||||
for k, v := range docs[0] {
|
||||
typename := k
|
||||
chanid := v.(string)
|
||||
gc.leaveRoom(chanid, ctx.CallBy.Accid)
|
||||
if k == "public" {
|
||||
gc.rh.JSONNumIncrBy(chanid, "$.size", -1)
|
||||
} else {
|
||||
logger.Println("chatting channel not valid :", chanid)
|
||||
}
|
||||
|
||||
case "LeavePublicChannel":
|
||||
chanid := args[0].(string)
|
||||
gc.rh.HDel(gc.rh.Context(), accidHex(sender.Accid), "cc_pub")
|
||||
gc.leaveRoom(chanid, sender.Accid)
|
||||
if f := sender.PopDisconnectedCallback(chanid); f != nil {
|
||||
f()
|
||||
}
|
||||
|
||||
case "TextMessage":
|
||||
chanid := args[0].(string)
|
||||
msg := args[1].(string)
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"sender": sender.Alias, "msg": msg},
|
||||
Tag: []string{"TextMessage"},
|
||||
})
|
||||
|
||||
case "EnterPrivateChannel":
|
||||
typename := args[0].(string)
|
||||
channel := args[1].(string)
|
||||
var reason string
|
||||
if len(args) > 2 {
|
||||
reason = args[2].(string)
|
||||
}
|
||||
|
||||
if len(reason) > 0 {
|
||||
// 수락
|
||||
ok, err := gc.rh.HSetNX(gc.rh.Context(), accidHex(sender.Accid), "cc_"+typename, channel).Result()
|
||||
if err != nil || !ok {
|
||||
// 이미 다른 private channel 참여 중
|
||||
logger.Println("EnterPrivateChannel failed. HSetNX return err :", err, sender.Accid.Hex(), typename, channel)
|
||||
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{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"sender": ctx.CallBy.Alias, "typename": typename},
|
||||
Tag: []string{"LeavePrivateChannel"},
|
||||
})
|
||||
}
|
||||
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + channel,
|
||||
Body: map[string]any{
|
||||
"sender": sender.Alias,
|
||||
"msg": reason,
|
||||
"typename": typename,
|
||||
},
|
||||
Tag: []string{"EnterPrivateChannel"},
|
||||
})
|
||||
|
||||
case "LeavePrivateChannel":
|
||||
channel := args[1].(string)
|
||||
gc.leaveRoom(channel, sender.Accid)
|
||||
if f := sender.PopDisconnectedCallback(channel); f != nil {
|
||||
f()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (gc *groupChat) EnterPublicChannel(ctx wshandler.ApiCallContext) {
|
||||
chanid := ctx.Arguments[0].(string)
|
||||
if cfg, ok := gc.chatConfig.Channels[chanid]; ok {
|
||||
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 {
|
||||
// 입장
|
||||
newsize, err := gc.rh.JSONNumIncrBy(chanid, "$.size", 1)
|
||||
if err == nil {
|
||||
gc.enterRoom(chanid, ctx.CallBy.Accid)
|
||||
gc.rh.JSONSet(ctx.CallBy.Accid.Hex(), "$.channel.public", chanid)
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"size": newsize[0]},
|
||||
Tag: []string{"ChattingChannelProperties"},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// 풀방
|
||||
logger.Println("chatting channel is full :", chanid, size, cfg.Capacity)
|
||||
}
|
||||
} else {
|
||||
logger.Println("chatting channel not valid :", chanid)
|
||||
}
|
||||
}
|
||||
|
||||
func (gc *groupChat) LeavePublicChannel(ctx wshandler.ApiCallContext) {
|
||||
chanid := ctx.Arguments[0].(string)
|
||||
cnt, _ := gc.rh.JSONDel(ctx.CallBy.Accid.Hex(), "$.channel.public")
|
||||
if cnt > 0 {
|
||||
gc.leaveRoom(chanid, ctx.CallBy.Accid)
|
||||
gc.rh.JSONNumIncrBy(chanid, "$.size", -1)
|
||||
}
|
||||
}
|
||||
|
||||
func (gc *groupChat) TextMessage(ctx wshandler.ApiCallContext) {
|
||||
chanid := ctx.Arguments[0].(string)
|
||||
msg := ctx.Arguments[1].(string)
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"sender": ctx.CallBy.Alias, "msg": msg},
|
||||
Tag: []string{"TextMessage"},
|
||||
})
|
||||
}
|
||||
|
||||
func (gc *groupChat) EnterPrivateChannel(ctx wshandler.ApiCallContext) {
|
||||
typename := ctx.Arguments[0].(string)
|
||||
channel := ctx.Arguments[1].(string)
|
||||
var reason string
|
||||
if len(ctx.Arguments) > 2 {
|
||||
reason = ctx.Arguments[2].(string)
|
||||
}
|
||||
|
||||
if len(reason) > 0 {
|
||||
// 수락
|
||||
ok, err := gc.rh.JSONSet(ctx.CallBy.Accid.Hex(), "$.channel."+typename, channel, gocommon.RedisonSetOptionNX)
|
||||
if err != nil || !ok {
|
||||
// 이미 다른 private channel 참여 중
|
||||
logger.Println("EnterPrivateChannel failed. HSetNX return err :", err, ctx.CallBy.Accid.Hex(), typename, channel)
|
||||
return
|
||||
}
|
||||
gc.enterRoom(channel, ctx.CallBy.Accid)
|
||||
} else {
|
||||
// 내가 이미 private channel에 있다는 것을 다른 사람들에게 알려주기 위함
|
||||
}
|
||||
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + channel,
|
||||
Body: map[string]any{
|
||||
"sender": ctx.CallBy.Alias,
|
||||
"msg": reason,
|
||||
"typename": typename,
|
||||
},
|
||||
Tag: []string{"EnterPrivateChannel"},
|
||||
})
|
||||
}
|
||||
|
||||
func (gc *groupChat) LeavePrivateChannel(ctx wshandler.ApiCallContext) {
|
||||
typename := ctx.Arguments[0].(string)
|
||||
chanid := ctx.Arguments[1].(string)
|
||||
cnt, _ := gc.rh.JSONDel(ctx.CallBy.Accid.Hex(), "$.channel."+typename)
|
||||
if cnt > 0 {
|
||||
gc.leaveRoom(chanid, ctx.CallBy.Accid)
|
||||
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + chanid,
|
||||
Body: map[string]any{"sender": ctx.CallBy.Alias, "typename": typename},
|
||||
Tag: []string{"LeavePrivateChannel"},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandler.WebSocketMessageType, message any) {
|
||||
// if mt == wshandler.Disconnected {
|
||||
// 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)
|
||||
// args := commandline[1:]
|
||||
// switch cmd {
|
||||
// case "EnterPublicChannel":
|
||||
// chanid := args[0].(string)
|
||||
// if cfg, ok := gc.chatConfig.Channels[chanid]; ok {
|
||||
// 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 {
|
||||
// // 입장
|
||||
// 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,
|
||||
// Body: map[string]any{"size": newsize[0]},
|
||||
// Tag: []string{"ChattingChannelProperties"},
|
||||
// })
|
||||
// }
|
||||
// } else {
|
||||
// // 풀방
|
||||
// logger.Println("chatting channel is full :", chanid, size, cfg.Capacity)
|
||||
// }
|
||||
// } else {
|
||||
// logger.Println("chatting channel not valid :", chanid)
|
||||
// }
|
||||
|
||||
// case "LeavePublicChannel":
|
||||
// chanid := args[0].(string)
|
||||
// gc.rh.HDel(gc.rh.Context(), accidHex(sender.Accid), "cc_pub")
|
||||
// gc.leaveRoom(chanid, sender.Accid)
|
||||
// if f := sender.PopDisconnectedCallback(chanid); f != nil {
|
||||
// f()
|
||||
// }
|
||||
|
||||
// case "TextMessage":
|
||||
// chanid := args[0].(string)
|
||||
// msg := args[1].(string)
|
||||
// gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
// Target: "#" + chanid,
|
||||
// Body: map[string]any{"sender": sender.Alias, "msg": msg},
|
||||
// Tag: []string{"TextMessage"},
|
||||
// })
|
||||
|
||||
// case "EnterPrivateChannel":
|
||||
// typename := args[0].(string)
|
||||
// channel := args[1].(string)
|
||||
// var reason string
|
||||
// if len(args) > 2 {
|
||||
// reason = args[2].(string)
|
||||
// }
|
||||
|
||||
// if len(reason) > 0 {
|
||||
// // 수락
|
||||
// // 이거 HSet 하면 안되겠는데? JSONSet해야할 듯?
|
||||
// ok, err := gc.rh.HSetNX(gc.rh.Context(), accidHex(sender.Accid), "cc_"+typename, channel).Result()
|
||||
// if err != nil || !ok {
|
||||
// // 이미 다른 private channel 참여 중
|
||||
// logger.Println("EnterPrivateChannel failed. HSetNX return err :", err, sender.Accid.Hex(), typename, channel)
|
||||
// return
|
||||
// }
|
||||
// gc.enterRoom(channel, sender.Accid)
|
||||
|
||||
// sender.RegistDisconnectedCallback(channel, func() {
|
||||
// gc.rh.JSONDel(channel, "$."+sender.Accid.Hex())
|
||||
// // 이거 HDel 하면 안되겠는데? JSONDel해야할 듯?
|
||||
// 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"},
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
// } else {
|
||||
// // 내가 이미 private channel에 있다는 것을 다른 사람들에게 알려주기 위함
|
||||
// }
|
||||
|
||||
// gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
// Target: "#" + channel,
|
||||
// Body: map[string]any{
|
||||
// "sender": sender.Alias,
|
||||
// "msg": reason,
|
||||
// "typename": typename,
|
||||
// },
|
||||
// Tag: []string{"EnterPrivateChannel"},
|
||||
// })
|
||||
|
||||
// case "LeavePrivateChannel":
|
||||
// channel := args[1].(string)
|
||||
// gc.leaveRoom(channel, sender.Accid)
|
||||
// if f := sender.PopDisconnectedCallback(channel); f != nil {
|
||||
// f()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
func (gc *groupChat) FetchChattingChannels(w http.ResponseWriter, r *http.Request) {
|
||||
var data struct {
|
||||
Prefix string `bson:"prefix"`
|
||||
@ -271,38 +371,23 @@ func (gc *groupChat) QueryPlayerChattingChannel(w http.ResponseWriter, r *http.R
|
||||
Accid string `bson:"accid"`
|
||||
Typename string `bson:"typename"`
|
||||
}
|
||||
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil {
|
||||
err := gocommon.ReadBsonDocumentFromBody(r.Body, &data)
|
||||
if err != nil {
|
||||
logger.Println("QueryPlayerChattingChannel failed. ReadBsonDocumentFromBody returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
accid := data.Accid
|
||||
typename := data.Typename
|
||||
|
||||
var fields []string
|
||||
if len(typename) == 0 {
|
||||
fields = []string{"cc_pub"}
|
||||
} else {
|
||||
fields = []string{"cc_pub", "cc_" + typename}
|
||||
}
|
||||
|
||||
chans, err := gc.rh.HMGet(gc.rh.Context(), accidstrHex(accid), fields...).Result()
|
||||
sub, err := gc.rh.JSONGetDocuments(accid, "$.channel")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
output := make(map[string]string)
|
||||
if len(chans) > 0 && chans[0] != nil {
|
||||
output["public"] = chans[0].(string)
|
||||
if len(sub) > 0 {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.Encode(sub[0])
|
||||
}
|
||||
if len(chans) > 1 && chans[1] != nil {
|
||||
output[typename] = chans[1].(string)
|
||||
}
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
enc.Encode(output)
|
||||
}
|
||||
|
||||
func (gc *groupChat) SendMessageOnChannel(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user