room 인원을 count에 저장

This commit is contained in:
2023-08-06 12:28:11 +09:00
parent d6738b2b70
commit 6f9f791f02
2 changed files with 7 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"github.com/go-redis/redis/v8"
"github.com/gorilla/websocket"
"repositories.action2quare.com/ayo/gocommon/logger"
)
@ -16,10 +17,11 @@ type room struct {
name string
destroyChan chan<- string
sendMsgChan chan<- send_msg_queue_elem
redisClient *redis.Client
}
// 만약 destroyChan가 nil이면 room이 비어도 파괴되지 않는다. 영구 유지되는 room
func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room {
func makeRoom(name string, redisClient *redis.Client, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room {
return &room{
inChan: make(chan *wsconn, 10),
outChan: make(chan *wsconn, 10),
@ -27,6 +29,7 @@ func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_ms
name: name,
destroyChan: destroyChan,
sendMsgChan: sendMsgChan,
redisClient: redisClient,
}
}
@ -70,9 +73,11 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b
return true
case conn := <-r.inChan:
r.redisClient.HIncrBy(ctx, r.name, "count", 1).Result()
(*conns)[conn.sender.Accid.Hex()] = conn
case conn := <-r.outChan:
r.redisClient.HIncrBy(ctx, r.name, "count", -1).Result()
delete((*conns), conn.sender.Accid.Hex())
if len(*conns) == 0 && r.destroyChan != nil {
r.destroyChan <- r.name