room과 conn의 상호 의존성 제거
This commit is contained in:
@ -13,7 +13,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"repositories.action2quare.com/ayo/gocommon"
|
"repositories.action2quare.com/ayo/gocommon"
|
||||||
@ -29,31 +28,7 @@ var noAuthFlag = flagx.Bool("noauth", false, "")
|
|||||||
|
|
||||||
type wsconn struct {
|
type wsconn struct {
|
||||||
*websocket.Conn
|
*websocket.Conn
|
||||||
sender *Sender
|
sender *Sender
|
||||||
joinedRooms []*room
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *wsconn) popRoom(r *room) int {
|
|
||||||
for i, jr := range conn.joinedRooms {
|
|
||||||
if jr == r {
|
|
||||||
conn.joinedRooms = append(conn.joinedRooms[:i], conn.joinedRooms[i+1:]...)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return len(conn.joinedRooms)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *wsconn) pushRoom(r *room) {
|
|
||||||
conn.joinedRooms = append(conn.joinedRooms, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *wsconn) isInRoom(roomname string) bool {
|
|
||||||
for _, r := range conn.joinedRooms {
|
|
||||||
if r.name == roomname {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpstreamMessage struct {
|
type UpstreamMessage struct {
|
||||||
@ -312,7 +287,7 @@ func (ws *WebsocketHandler) mainLoop(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(roomid) > 0 {
|
if len(roomid) > 0 {
|
||||||
if !conn.isInRoom(roomid) {
|
if room := findRoom(roomid, false); room == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -344,7 +319,7 @@ func (ws *WebsocketHandler) mainLoop(ctx context.Context) {
|
|||||||
if conn == nil {
|
if conn == nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
conn.pushRoom(findRoom(roomName, true).in(conn))
|
findRoom(roomName, true).in(conn)
|
||||||
|
|
||||||
case commandType_LeaveRoom:
|
case commandType_LeaveRoom:
|
||||||
if len(usermsg.Args) != 2 {
|
if len(usermsg.Args) != 2 {
|
||||||
@ -362,10 +337,7 @@ func (ws *WebsocketHandler) mainLoop(ctx context.Context) {
|
|||||||
return false, errProcessFailed_NotInRoom
|
return false, errProcessFailed_NotInRoom
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn.popRoom(room.out(conn)) == 0 {
|
room.out(conn)
|
||||||
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
|
|
||||||
conn.WriteControl(websocket.CloseMessage, closeMsg, time.Time{})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@ -448,10 +420,6 @@ func (ws *WebsocketHandler) mainLoop(ctx context.Context) {
|
|||||||
case c := <-ws.connInOutChan:
|
case c := <-ws.connInOutChan:
|
||||||
if c.Conn == nil {
|
if c.Conn == nil {
|
||||||
delete(entireConns, c.sender.Accid.Hex())
|
delete(entireConns, c.sender.Accid.Hex())
|
||||||
for _, room := range c.joinedRooms {
|
|
||||||
room.out(c)
|
|
||||||
}
|
|
||||||
c.joinedRooms = nil
|
|
||||||
go ws.wsApiBroker.Call(c.sender, ClientDisconnected, nil)
|
go ws.wsApiBroker.Call(c.sender, ClientDisconnected, nil)
|
||||||
} else {
|
} else {
|
||||||
entireConns[c.sender.Accid.Hex()] = c
|
entireConns[c.sender.Accid.Hex()] = c
|
||||||
|
|||||||
Reference in New Issue
Block a user