Files
gocommon/wshandler/room.go
mountain e0e911f9e7 Squashed commit of the following:
commit 29b2f25850
Author: mountain <mountain@action2quare.com>
Date:   Wed Jul 19 09:33:37 2023 +0900

    타입 이름 변경

commit 256bfd030c
Author: mountain <mountain@action2quare.com>
Date:   Wed Jul 19 09:31:01 2023 +0900

    redison 추가

commit 72a683fed2
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 19:51:24 2023 +0900

    gob에 []any 추가

commit 89fa9e4ac5
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 17:45:12 2023 +0900

    write control 수정

commit d724cc84fa
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 17:38:04 2023 +0900

    redis pubsub 채널 이름에 디비 인덱스 추가

commit 8df248fa54
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 17:20:47 2023 +0900

    close를 writecontrol로 변경

commit 40a603522d
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 12:21:06 2023 +0900

    conn에 msg를 쓰는 쓰레드 단일화

commit c21017d2cd
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 11:08:38 2023 +0900

    redis call이 문제가 아니었음

commit 82abcddb49
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 11:04:15 2023 +0900

    잦은 redis call 회피

commit 289af24a8f
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 09:55:18 2023 +0900

    room create 메시지 전송

commit 4b35e0e638
Author: mountain <mountain@action2quare.com>
Date:   Tue Jul 18 09:45:27 2023 +0900

    EventReceiver 인터페이스 추가

commit 29843802ff
Author: mountain <mountain@action2quare.com>
Date:   Mon Jul 17 17:45:40 2023 +0900

    gob 등록

commit 66aea48fb7
Author: mountain <mountain@action2quare.com>
Date:   Sun Jul 16 18:39:11 2023 +0900

    채널간 publish marshalling을 gob으로 변경

commit 8f6c87a8ae
Author: mountain <mountain@action2quare.com>
Date:   Sun Jul 16 16:37:02 2023 +0900

    redis option을 copy로 변경

commit f0f459332d
Author: mountain <mountain@action2quare.com>
Date:   Sat Jul 15 17:08:33 2023 +0900

    wshandler에서 authcache제거하고 config 포맷 변경
2023-07-19 09:35:25 +09:00

99 lines
2.0 KiB
Go

package wshandler
import (
"context"
"encoding/json"
"github.com/gorilla/websocket"
"repositories.action2quare.com/ayo/gocommon/logger"
)
type room struct {
inChan chan *wsconn
outChan chan *wsconn
messageChan chan *UpstreamMessage
name string
destroyChan chan<- string
sendMsgChan chan<- send_msg_queue_elem
}
// 만약 destroyChan가 nil이면 room이 비어도 파괴되지 않는다. 영구 유지되는 room
func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room {
return &room{
inChan: make(chan *wsconn, 10),
outChan: make(chan *wsconn, 10),
messageChan: make(chan *UpstreamMessage, 100),
name: name,
destroyChan: destroyChan,
sendMsgChan: sendMsgChan,
}
}
func (r *room) broadcast(msg *UpstreamMessage) {
r.messageChan <- msg
}
func (r *room) in(conn *wsconn) *room {
r.inChan <- conn
return r
}
func (r *room) out(conn *wsconn) *room {
r.outChan <- conn
return r
}
func (r *room) start(ctx context.Context) {
go func(ctx context.Context) {
conns := make(map[string]*wsconn)
normal := false
for !normal {
normal = r.loop(ctx, &conns)
}
}(ctx)
}
func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd bool) {
defer func() {
s := recover()
if s != nil {
logger.Error(s)
normalEnd = false
}
}()
tag := "#" + r.name
for {
select {
case <-ctx.Done():
return true
case conn := <-r.inChan:
(*conns)[conn.sender.Accid.Hex()] = conn
case conn := <-r.outChan:
delete((*conns), conn.sender.Accid.Hex())
if len(*conns) == 0 && r.destroyChan != nil {
r.destroyChan <- r.name
return true
}
case msg := <-r.messageChan:
ds := DownstreamMessage{
Alias: msg.Alias,
Body: msg.Body,
Tag: append(msg.Tag, tag),
}
bt, _ := json.Marshal(ds)
for _, conn := range *conns {
r.sendMsgChan <- send_msg_queue_elem{
to: conn,
mt: websocket.TextMessage,
msg: bt,
}
}
}
}
}