세션 해제 콜백 추가
This commit is contained in:
@ -22,10 +22,22 @@ 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 {
|
||||
@ -140,14 +152,28 @@ func NewWebsocketPeerHandler[T PeerInterface](consumer session.Consumer, creator
|
||||
methods[k] = v
|
||||
}
|
||||
|
||||
return &websocketPeerHandler[T]{
|
||||
wsh := &websocketPeerHandler[T]{
|
||||
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_nosession)
|
||||
} else {
|
||||
@ -157,14 +183,33 @@ 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