ClientConnect signature 변경

This commit is contained in:
2023-09-19 18:46:41 +09:00
parent 675bcbad9e
commit 6706d7d02e
7 changed files with 87 additions and 182 deletions

View File

@ -1 +1,49 @@
{} {
"region_storage": {
"default": {
"mongo": "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false",
"redis": {
"cache": "redis://192.168.8.94:6380/0",
"session": "redis://192.168.8.94:6380/1",
"tx": "redis://192.168.8.94:6380/2",
"tavern": "redis://192.168.8.94:6380/3",
"wshandler": "redis://192.168.8.94:6380/4"
}
}
},
"session_storage": "redis://192.168.8.94:6380/3",
"session_ttl": 3600,
"maingate_session_storage": "redis://192.168.8.94:6380/1",
"maingate_session_ttl" : 3600,
"maingate_api_token": "63d08aa34f0162622c11284b",
"tavern_redis_url": "redis://192.168.8.94:6380/4",
"tavern_service_url": "http://localhost/tavern",
"tavern_group_types": {
"party": {
"max_member": 3,
"invite_ttl": 30
},
"chat" : {
"default_capacity" : 1000,
"channels" : {
"bazzar-1" : {
"name" : "FText(bazzar-1)"
},
"bazzar-2" : {
"name" : "FText(bazzar-2)"
},
"bazzar-3" : {
"name" : "FText(bazzar-3)"
},
"bazzar-4" : {
"name" : "FText(bazzar-4)"
},
"bazzar-5" : {
"name" : "FText(bazzar-5)"
}
}
}
}
}

View File

@ -11,10 +11,14 @@
} }
} }
}, },
"maingate_session_storage": "redis://192.168.8.94:6380/1", "session_storage": "redis://192.168.8.94:6380/5",
"session_ttl": 3600,
"maingate_session_storage": "redis://192.168.8.94:6380/6",
"maingate_session_ttl" : 3600, "maingate_session_ttl" : 3600,
"maingate_api_token": "63d08aa34f0162622c11284b", "maingate_api_token": "63d08aa34f0162622c11284b",
"tavern_redis_url": "redis://192.168.8.94:6380/7",
"tavern_service_url": "http://localhost/tavern", "tavern_service_url": "http://localhost/tavern",
"tavern_group_types": { "tavern_group_types": {
"party": { "party": {

View File

@ -8,6 +8,7 @@ import (
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"github.com/gorilla/websocket"
"repositories.action2quare.com/ayo/gocommon" "repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/gocommon/wshandler" "repositories.action2quare.com/ayo/gocommon/wshandler"
@ -76,25 +77,25 @@ func (gc *groupChat) Initialize(tv *Tavern, cfg configDocument) error {
return nil return nil
} }
func (gc *groupChat) ClientConnected(ctx wshandler.ApiCallContext) { func (gc *groupChat) ClientConnected(conn *websocket.Conn, callby *wshandler.Sender) {
gc.rh.JSONSet(ctx.CallBy.Accid.Hex(), "$.channel", map[string]any{}) gc.rh.JSONSet(callby.Accid.Hex(), "$.channel", map[string]any{})
} }
func (gc *groupChat) ClientDisconnected(ctx wshandler.ApiCallContext) { func (gc *groupChat) ClientDisconnected(conn *websocket.Conn, callby *wshandler.Sender) {
docs, _ := gc.rh.JSONGetDocuments(ctx.CallBy.Accid.Hex(), "$.channel") docs, _ := gc.rh.JSONGetDocuments(callby.Accid.Hex(), "$.channel")
if len(docs) > 0 { if len(docs) > 0 {
for k, v := range docs[0] { for k, v := range docs[0] {
typename := k typename := k
chanid := v.(string) chanid := v.(string)
gc.leaveRoom(chanid, ctx.CallBy.Accid) gc.leaveRoom(chanid, callby.Accid)
if k == "public" { if k == "public" {
gc.rh.JSONNumIncrBy(chanid, "$.size", -1) gc.rh.JSONNumIncrBy(chanid, "$.size", -1)
} else { } else {
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{ gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
Target: "#" + chanid, Target: "#" + chanid,
Body: map[string]any{"sender": ctx.CallBy.Alias, "typename": typename}, Body: map[string]any{"sender": callby.Alias},
Tag: []string{"LeavePrivateChannel"}, Tag: []string{typename + ".LeavePrivateChannel"},
}) })
} }
} }
@ -169,11 +170,10 @@ func (gc *groupChat) EnterPrivateChannel(ctx wshandler.ApiCallContext) {
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{ gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
Target: "#" + channel, Target: "#" + channel,
Body: map[string]any{ Body: map[string]any{
"sender": ctx.CallBy.Alias, "sender": ctx.CallBy.Alias,
"msg": reason, "msg": reason,
"typename": typename,
}, },
Tag: []string{"EnterPrivateChannel"}, Tag: []string{typename + ".EnterPrivateChannel"},
}) })
} }
@ -185,131 +185,12 @@ func (gc *groupChat) LeavePrivateChannel(ctx wshandler.ApiCallContext) {
gc.leaveRoom(chanid, ctx.CallBy.Accid) gc.leaveRoom(chanid, ctx.CallBy.Accid)
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{ gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
Target: "#" + chanid, Target: "#" + chanid,
Body: map[string]any{"sender": ctx.CallBy.Alias, "typename": typename}, Body: map[string]any{"sender": ctx.CallBy.Alias},
Tag: []string{"LeavePrivateChannel"}, Tag: []string{typename + ".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) { func (gc *groupChat) FetchChattingChannels(w http.ResponseWriter, r *http.Request) {
var prefix string var prefix string
if err := gocommon.MakeDecoder(r).Decode(&prefix); err != nil { if err := gocommon.MakeDecoder(r).Decode(&prefix); err != nil {

View File

@ -710,6 +710,7 @@ func (gp *groupParty) ClientDisconnected(ctx wshandler.ApiCallContext) {
Tag: []string{"MemberDocFull"}, Tag: []string{"MemberDocFull"},
}) })
} }
} }
@ -721,27 +722,9 @@ func (gp *groupParty) UpdatePartyMemberDocumentDirect(ctx wshandler.ApiCallConte
} }
func (gp *groupParty) UpdatePartyDocumentDirect(ctx wshandler.ApiCallContext) { func (gp *groupParty) UpdatePartyDocumentDirect(ctx wshandler.ApiCallContext) {
// 파티 오너만 가능
gidobj, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string)) gidobj, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string))
doc := ctx.Arguments[1].(map[string]any) doc := ctx.Arguments[1].(map[string]any)
gd := groupDoc{
id: gidobj,
rh: gp.rh,
}
incharge, err := gp.rh.JSONGet(gd.strid(), "$._incharge")
if err != nil {
logger.Println("UpdatePartyDocumentDirect failed. gp.rh.JSONGet returns err :", err)
return
}
if !strings.Contains(incharge.(string), gd.tid(ctx.CallBy.Accid)) {
// incharge가 아니네?
logger.Println("UpdatePartyDocumentDirect failed. caller is not incharge")
return
}
gp.updatePartyDocument(gidobj, doc) gp.updatePartyDocument(gidobj, doc)
} }

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"github.com/gorilla/websocket"
"repositories.action2quare.com/ayo/gocommon" "repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/flagx" "repositories.action2quare.com/ayo/gocommon/flagx"
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
@ -35,7 +36,7 @@ type Tavern struct {
wsh *wshandler.WebsocketHandler wsh *wshandler.WebsocketHandler
mongoClient gocommon.MongoClient mongoClient gocommon.MongoClient
redison *gocommon.RedisonHandler redison *gocommon.RedisonHandler
httpApiBorker gocommon.HttpApiHandlerContainer httpApiBorker gocommon.HttpApiBroker
} }
func getMacAddr() (string, error) { func getMacAddr() (string, error) {
@ -57,12 +58,12 @@ func getMacAddr() (string, error) {
// New : // New :
func New(context context.Context, wsh *wshandler.WebsocketHandler) (*Tavern, error) { func New(context context.Context, wsh *wshandler.WebsocketHandler) (*Tavern, error) {
if err := gocommon.LoadConfig(&config); err != nil { if err := gocommon.LoadConfig(&config); err != nil {
return nil, err return nil, logger.ErrorWithCallStack(err)
} }
macaddr, err := getMacAddr() macaddr, err := getMacAddr()
if err != nil { if err != nil {
return nil, err return nil, logger.ErrorWithCallStack(err)
} }
config.macAddr = macaddr config.macAddr = macaddr
tv := &Tavern{ tv := &Tavern{
@ -71,7 +72,7 @@ func New(context context.Context, wsh *wshandler.WebsocketHandler) (*Tavern, err
if err = tv.prepare(context); err != nil { if err = tv.prepare(context); err != nil {
logger.Println("tavern prepare() failed :", err) logger.Println("tavern prepare() failed :", err)
return nil, err return nil, logger.ErrorWithCallStack(err)
} }
return tv, nil return tv, nil
@ -84,28 +85,28 @@ func (tv *Tavern) Cleanup() {
func (tv *Tavern) prepare(ctx context.Context) error { func (tv *Tavern) prepare(ctx context.Context) error {
redisClient, err := gocommon.NewRedisClient(config.RedisURL) redisClient, err := gocommon.NewRedisClient(config.RedisURL)
if err != nil { if err != nil {
return err return logger.ErrorWithCallStack(err)
} }
tv.redison = gocommon.NewRedisonHandler(redisClient.Context(), redisClient) tv.redison = gocommon.NewRedisonHandler(redisClient.Context(), redisClient)
tv.wsh.RegisterApiHandler(wshandler.MakeWebsocketApiHandler(tv, "tv")) tv.wsh.AddHandler(wshandler.MakeWebsocketApiHandler(tv, "tv"))
if cfg, ok := config.Group["chat"]; ok { if cfg, ok := config.Group["chat"]; ok {
chat := new(groupChat) chat := new(groupChat)
if err := chat.Initialize(tv, cfg); err != nil { if err := chat.Initialize(tv, cfg); err != nil {
return err return logger.ErrorWithCallStack(err)
} }
tv.httpApiBorker.RegisterApiHandler(gocommon.MakeHttpApiHandler(chat, "chat")) tv.httpApiBorker.AddHandler(gocommon.MakeHttpApiHandler(chat, "chat"))
tv.wsh.RegisterApiHandler(wshandler.MakeWebsocketApiHandler(chat, "chat")) tv.wsh.AddHandler(wshandler.MakeWebsocketApiHandler(chat, "chat"))
} }
if cfg, ok := config.Group["party"]; ok { if cfg, ok := config.Group["party"]; ok {
party := new(groupParty) party := new(groupParty)
if err := party.Initialize(tv, cfg); err != nil { if err := party.Initialize(tv, cfg); err != nil {
return err return logger.ErrorWithCallStack(err)
} }
tv.httpApiBorker.RegisterApiHandler(gocommon.MakeHttpApiHandler(party, "party")) tv.httpApiBorker.AddHandler(gocommon.MakeHttpApiHandler(party, "party"))
tv.wsh.RegisterApiHandler(wshandler.MakeWebsocketApiHandler(party, "party")) tv.wsh.AddHandler(wshandler.MakeWebsocketApiHandler(party, "party"))
} }
return nil return nil
@ -127,18 +128,16 @@ func (tv *Tavern) LeaveChannel(ctx wshandler.ApiCallContext) {
tv.wsh.LeaveRoom(ctx.Arguments[0].(string), ctx.CallBy.Accid) tv.wsh.LeaveRoom(ctx.Arguments[0].(string), ctx.CallBy.Accid)
} }
func (tv *Tavern) ClientConnected(ctx wshandler.ApiCallContext) { func (tv *Tavern) ClientConnected(conn *websocket.Conn, callby *wshandler.Sender) {
logger.Println("ClientConnected :", ctx.CallBy.Alias) tv.redison.Del(tv.redison.Context(), callby.Accid.Hex())
tv.redison.Del(tv.redison.Context(), ctx.CallBy.Accid.Hex()) _, err := tv.redison.JSONSet(callby.Accid.Hex(), "$", bson.M{"_ts": time.Now().UTC().Unix()})
_, err := tv.redison.JSONSet(ctx.CallBy.Accid.Hex(), "$", bson.M{"_ts": time.Now().UTC().Unix()})
if err != nil { if err != nil {
logger.Println("OnClientMessageReceived HSet error :", err) logger.Println("OnClientMessageReceived HSet error :", err)
} }
} }
func (tv *Tavern) ClientDisconnected(ctx wshandler.ApiCallContext) { func (tv *Tavern) ClientDisconnected(conn *websocket.Conn, callby *wshandler.Sender) {
tv.redison.Del(tv.redison.Context(), ctx.CallBy.Accid.Hex()).Result() tv.redison.Del(tv.redison.Context(), callby.Accid.Hex()).Result()
logger.Println("ClientDisconnected :", ctx.CallBy.Alias)
} }
func (tv *Tavern) OnRoomCreated(name string) { func (tv *Tavern) OnRoomCreated(name string) {

2
go.mod
View File

@ -4,6 +4,7 @@ go 1.20
require ( require (
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
github.com/gorilla/websocket v1.5.0
go.mongodb.org/mongo-driver v1.11.7 go.mongodb.org/mongo-driver v1.11.7
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946 repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946
) )
@ -13,7 +14,6 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/snappy v0.0.4 // indirect github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.4 // indirect github.com/google/go-cmp v0.5.4 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/klauspost/compress v1.16.6 // indirect github.com/klauspost/compress v1.16.6 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect github.com/pires/go-proxyproto v0.7.0 // indirect

10
go.sum
View File

@ -104,15 +104,5 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230906142024-eb54fa2e3a44 h1:90XY5WSLtxvfi6YktDY4Sv1CMPRViZvPLPunA1eIxZA=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230906142024-eb54fa2e3a44/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908023557-6cbf32c3868b h1:Rx6tP6IhlGlVGGgMDZ7OuIDU9cHfvm2L05L2tqF7G58=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908023557-6cbf32c3868b/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908025007-3603c0386b29 h1:Ts40m9MLMMx4uaQWko5QXkg/HX4uYQB9TGGEN6twhiU=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908025007-3603c0386b29/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908062630-46ce5f09897a h1:xKUI2xlP6LcUV5fy+4QEHoaZOhkSsMYgeIp6H5ADBCM=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908062630-46ce5f09897a/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908091916-23231dc6d705 h1:sK2mbRwqTMTZFmP9F50MIFZG9hcQ+EeW7tsGTzBgAow=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908091916-23231dc6d705/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946 h1:YSvgTNuHeKis37+FfOvzVLYCaXQ0oF+CWBTy4bRqq3g= repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946 h1:YSvgTNuHeKis37+FfOvzVLYCaXQ0oF+CWBTy4bRqq3g=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY= repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=