room 인원을 count에 저장
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||||
)
|
)
|
||||||
@ -16,10 +17,11 @@ type room struct {
|
|||||||
name string
|
name string
|
||||||
destroyChan chan<- string
|
destroyChan chan<- string
|
||||||
sendMsgChan chan<- send_msg_queue_elem
|
sendMsgChan chan<- send_msg_queue_elem
|
||||||
|
redisClient *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// 만약 destroyChan가 nil이면 room이 비어도 파괴되지 않는다. 영구 유지되는 room
|
// 만약 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{
|
return &room{
|
||||||
inChan: make(chan *wsconn, 10),
|
inChan: make(chan *wsconn, 10),
|
||||||
outChan: 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,
|
name: name,
|
||||||
destroyChan: destroyChan,
|
destroyChan: destroyChan,
|
||||||
sendMsgChan: sendMsgChan,
|
sendMsgChan: sendMsgChan,
|
||||||
|
redisClient: redisClient,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,9 +73,11 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b
|
|||||||
return true
|
return true
|
||||||
|
|
||||||
case conn := <-r.inChan:
|
case conn := <-r.inChan:
|
||||||
|
r.redisClient.HIncrBy(ctx, r.name, "count", 1).Result()
|
||||||
(*conns)[conn.sender.Accid.Hex()] = conn
|
(*conns)[conn.sender.Accid.Hex()] = conn
|
||||||
|
|
||||||
case conn := <-r.outChan:
|
case conn := <-r.outChan:
|
||||||
|
r.redisClient.HIncrBy(ctx, r.name, "count", -1).Result()
|
||||||
delete((*conns), conn.sender.Accid.Hex())
|
delete((*conns), conn.sender.Accid.Hex())
|
||||||
if len(*conns) == 0 && r.destroyChan != nil {
|
if len(*conns) == 0 && r.destroyChan != nil {
|
||||||
r.destroyChan <- r.name
|
r.destroyChan <- r.name
|
||||||
|
|||||||
@ -362,7 +362,7 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
findRoom := func(name string, create bool) *room {
|
findRoom := func(name string, create bool) *room {
|
||||||
room := rooms[name]
|
room := rooms[name]
|
||||||
if room == nil && create {
|
if room == nil && create {
|
||||||
room = makeRoom(name, roomDestroyChan, sh.sendMsgChan)
|
room = makeRoom(name, sh.redisSync, roomDestroyChan, sh.sendMsgChan)
|
||||||
rooms[name] = room
|
rooms[name] = room
|
||||||
room.start(ctx)
|
room.start(ctx)
|
||||||
go sh.callReceiver.OnRoomCreated(sh.region, name)
|
go sh.callReceiver.OnRoomCreated(sh.region, name)
|
||||||
|
|||||||
Reference in New Issue
Block a user