diff --git a/internal/config/session.go b/internal/config/session.go index 2b06895f..33ebc50e 100644 --- a/internal/config/session.go +++ b/internal/config/session.go @@ -6,8 +6,9 @@ import ( ) type Session struct { - Password string - AdminPassword string + Password string + AdminPassword string + ImplicitHosting bool } func (Session) Init(cmd *cobra.Command) error { @@ -21,10 +22,16 @@ func (Session) Init(cmd *cobra.Command) error { return err } + cmd.PersistentFlags().Bool("implicit_hosting", true, "allow implicit control switching") + if err := viper.BindPFlag("implicit_hosting", cmd.PersistentFlags().Lookup("implicit_hosting")); err != nil { + return err + } + return nil } func (s *Session) Set() { s.Password = viper.GetString("password") s.AdminPassword = viper.GetString("password_admin") + s.ImplicitHosting = viper.GetBool("implicit_hosting") } diff --git a/internal/session/manager.go b/internal/session/manager.go index 7803c144..c133872d 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -241,3 +241,7 @@ func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Ses listener(payload[0].(*SessionCtx)) }) } + +func (manager *SessionManagerCtx) ImplicitHosting() bool { + return manager.config.ImplicitHosting +} diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index 37b16bb9..7fddbb3c 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -15,6 +15,7 @@ type SystemInit struct { ControlHost ControlHost `json:"control_host"` ScreenSize ScreenSize `json:"screen_size"` Members map[string]MemberData `json:"members"` + ImplicitHosting bool `json:"implicit_hosting"` } type SystemAdmin struct { diff --git a/internal/types/session.go b/internal/types/session.go index c28475a7..0233d2c9 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -48,6 +48,7 @@ type SessionManager interface { OnHostCleared(listener func(session Session)) OnConnected(listener func(session Session)) OnDisconnected(listener func(session Session)) + ImplicitHosting() bool // auth Authenticate(r *http.Request) (Session, error) diff --git a/internal/websocket/handler/control.go b/internal/websocket/handler/control.go index 1f4f0026..6428311e 100644 --- a/internal/websocket/handler/control.go +++ b/internal/websocket/handler/control.go @@ -30,16 +30,16 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error { return nil } - // TODO: Allow implicit requests. - host := h.sessions.GetHost() - if host != nil { - // tell session there is a host - return session.Send( - message.ControlHost{ - Event: event.CONTROL_HOST, - HasHost: true, - HostID: host.ID(), - }) + if !h.sessions.ImplicitHosting() { + // tell session if there is a host + if host := h.sessions.GetHost(); host != nil { + return session.Send( + message.ControlHost{ + Event: event.CONTROL_HOST, + HasHost: true, + HostID: host.ID(), + }) + } } h.sessions.SetHost(session) diff --git a/internal/websocket/handler/system.go b/internal/websocket/handler/system.go index 81684499..57876a16 100644 --- a/internal/websocket/handler/system.go +++ b/internal/websocket/handler/system.go @@ -34,15 +34,16 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error { return session.Send( message.SystemInit{ - Event: event.SYSTEM_INIT, - MemberId: session.ID(), - ControlHost: controlHost, - Members: members, - ScreenSize: message.ScreenSize{ + Event: event.SYSTEM_INIT, + MemberId: session.ID(), + ControlHost: controlHost, + ScreenSize: message.ScreenSize{ Width: size.Width, Height: size.Height, Rate: int(size.Rate), }, + Members: members, + ImplicitHosting: h.sessions.ImplicitHosting(), }) }