생성 삭제 채널을 밖으로 뺌
This commit is contained in:
@ -22,22 +22,10 @@ type WebsocketPeerHandler interface {
|
||||
RegisterHandlers(serveMux *http.ServeMux, prefix string) error
|
||||
}
|
||||
|
||||
type connEstChannelValue struct {
|
||||
accid primitive.ObjectID
|
||||
conn *websocket.Conn
|
||||
}
|
||||
|
||||
type connDisChannelValue struct {
|
||||
accid primitive.ObjectID
|
||||
closed bool
|
||||
}
|
||||
|
||||
type websocketPeerHandler[T PeerInterface] struct {
|
||||
methods map[string]peerApiFuncType[T]
|
||||
createPeer func(primitive.ObjectID) T
|
||||
sessionConsumer session.Consumer
|
||||
connEstChannel chan connEstChannelValue
|
||||
connDisChannel chan connDisChannelValue
|
||||
}
|
||||
|
||||
type PeerInterface interface {
|
||||
@ -156,24 +144,12 @@ func NewWebsocketPeerHandler[T PeerInterface](consumer session.Consumer, creator
|
||||
sessionConsumer: consumer,
|
||||
methods: methods,
|
||||
createPeer: creator,
|
||||
connEstChannel: make(chan connEstChannelValue),
|
||||
connDisChannel: make(chan connDisChannelValue),
|
||||
}
|
||||
|
||||
consumer.RegisterOnSessionInvalidated(wsh.onSessionInvalidated)
|
||||
return wsh
|
||||
}
|
||||
|
||||
func (ws *websocketPeerHandler[T]) onSessionInvalidated(accid primitive.ObjectID) {
|
||||
ws.connDisChannel <- connDisChannelValue{
|
||||
accid: accid,
|
||||
closed: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *websocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
|
||||
go ws.sessionMonitoring()
|
||||
|
||||
if *noAuthFlag {
|
||||
serveMux.HandleFunc(prefix, ws.upgrade_noauth)
|
||||
} else {
|
||||
@ -183,33 +159,14 @@ func (ws *websocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, pre
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *websocketPeerHandler[T]) sessionMonitoring() {
|
||||
all := make(map[primitive.ObjectID]*websocket.Conn)
|
||||
for {
|
||||
select {
|
||||
case estVal := <-ws.connEstChannel:
|
||||
all[estVal.accid] = estVal.conn
|
||||
case disVal := <-ws.connDisChannel:
|
||||
if disVal.closed {
|
||||
delete(all, disVal.accid)
|
||||
} else if c := all[disVal.accid]; c != nil {
|
||||
c.Close()
|
||||
delete(all, disVal.accid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *websocketPeerHandler[T]) upgrade_core(conn *websocket.Conn, accid primitive.ObjectID, nonce uint32) {
|
||||
go func(c *websocket.Conn, accid primitive.ObjectID) {
|
||||
peer := ws.createPeer(accid)
|
||||
var closeReason string
|
||||
|
||||
peer.ClientConnected(conn)
|
||||
ws.connEstChannel <- connEstChannelValue{accid: accid, conn: conn}
|
||||
|
||||
defer func() {
|
||||
ws.connDisChannel <- connDisChannelValue{accid: accid, closed: true}
|
||||
peer.ClientDisconnected(closeReason)
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user