diff --git a/internal/session/session.go b/internal/session/session.go index f5118407..716f1115 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -98,7 +98,13 @@ func (session *SessionCtx) ConnectWebSocketPeer(websocketPeer types.WebSocketPee } session.logger.Info().Msg("set websocket connected") + + // update state + now := time.Now() session.state.IsConnected = true + session.state.ConnectedSince = &now + session.state.NotConnectedSince = nil + session.manager.emmiter.Emit("connected", session) // if there is a previous peer, destroy it @@ -154,7 +160,12 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket // session.logger.Info().Msg("set websocket disconnected") + + now := time.Now() session.state.IsConnected = false + session.state.ConnectedSince = nil + session.state.NotConnectedSince = &now + session.manager.emmiter.Emit("disconnected", session) session.websocketMu.Lock() @@ -235,7 +246,16 @@ func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, conne Bool("connected", connected). Msg("set webrtc connected") + // update state session.state.IsWatching = connected + if now := time.Now(); connected { + session.state.WatchingSince = &now + session.state.NotWatchingSince = nil + } else { + session.state.WatchingSince = nil + session.state.NotWatchingSince = &now + } + session.manager.emmiter.Emit("state_changed", session) if connected { diff --git a/pkg/types/session.go b/pkg/types/session.go index a872587e..e901ad7a 100644 --- a/pkg/types/session.go +++ b/pkg/types/session.go @@ -3,6 +3,7 @@ package types import ( "errors" "net/http" + "time" ) var ( @@ -25,7 +26,16 @@ type SessionProfile struct { type SessionState struct { IsConnected bool `json:"is_connected"` - IsWatching bool `json:"is_watching"` + // when the session was last connected + ConnectedSince *time.Time `json:"connected_since,omitempty"` + // when the session was last not connected + NotConnectedSince *time.Time `json:"not_connected_since,omitempty"` + + IsWatching bool `json:"is_watching"` + // when the session was last watching + WatchingSince *time.Time `json:"watching_since,omitempty"` + // when the session was last not watching + NotWatchingSince *time.Time `json:"not_watching_since,omitempty"` } type Settings struct {