inactive cursors to config.

This commit is contained in:
Miroslav Šedivý 2021-11-01 17:49:02 +01:00
parent 28c22a254b
commit 3528766e7f
5 changed files with 56 additions and 34 deletions

View File

@ -9,6 +9,7 @@ import (
type Session struct { type Session struct {
ImplicitHosting bool ImplicitHosting bool
InactiveCursors bool
MercifulReconnect bool MercifulReconnect bool
APIToken string APIToken string
@ -24,6 +25,11 @@ func (Session) Init(cmd *cobra.Command) error {
return err return err
} }
cmd.PersistentFlags().Bool("session.inactive_cursors", true, "show inactive cursors on the screen")
if err := viper.BindPFlag("session.inactive_cursors", cmd.PersistentFlags().Lookup("session.inactive_cursors")); err != nil {
return err
}
cmd.PersistentFlags().Bool("session.merciful_reconnect", true, "allow reconnecting to websocket even if previous connection was not closed") cmd.PersistentFlags().Bool("session.merciful_reconnect", true, "allow reconnecting to websocket even if previous connection was not closed")
if err := viper.BindPFlag("session.merciful_reconnect", cmd.PersistentFlags().Lookup("session.merciful_reconnect")); err != nil { if err := viper.BindPFlag("session.merciful_reconnect", cmd.PersistentFlags().Lookup("session.merciful_reconnect")); err != nil {
return err return err
@ -60,6 +66,7 @@ func (Session) Init(cmd *cobra.Command) error {
func (s *Session) Set() { func (s *Session) Set() {
s.ImplicitHosting = viper.GetBool("session.implicit_hosting") s.ImplicitHosting = viper.GetBool("session.implicit_hosting")
s.InactiveCursors = viper.GetBool("session.inactive_cursors")
s.MercifulReconnect = viper.GetBool("session.merciful_reconnect") s.MercifulReconnect = viper.GetBool("session.merciful_reconnect")
s.APIToken = viper.GetString("session.api_token") s.APIToken = viper.GetString("session.api_token")

View File

@ -312,6 +312,10 @@ func (manager *SessionManagerCtx) ImplicitHosting() bool {
return manager.config.ImplicitHosting return manager.config.ImplicitHosting
} }
func (manager *SessionManagerCtx) InactiveCursors() bool {
return manager.config.InactiveCursors
}
func (manager *SessionManagerCtx) CookieEnabled() bool { func (manager *SessionManagerCtx) CookieEnabled() bool {
return manager.config.CookieEnabled return manager.config.CookieEnabled
} }

View File

@ -54,7 +54,9 @@ func (session *SessionCtx) IsHost() bool {
} }
func (session *SessionCtx) SetCursor(cursor types.Cursor) { func (session *SessionCtx) SetCursor(cursor types.Cursor) {
session.manager.SetCursor(cursor, session) if session.manager.InactiveCursors() {
session.manager.SetCursor(cursor, session)
}
} }
// --- // ---

View File

@ -70,6 +70,7 @@ type SessionManager interface {
OnHostChanged(listener func(session Session)) OnHostChanged(listener func(session Session))
ImplicitHosting() bool ImplicitHosting() bool
InactiveCursors() bool
CookieEnabled() bool CookieEnabled() bool
MercifulReconnect() bool MercifulReconnect() bool

View File

@ -20,6 +20,9 @@ import (
// send pings to peer with this period - must be less than pongWait // send pings to peer with this period - must be less than pongWait
const pingPeriod = 10 * time.Second const pingPeriod = 10 * time.Second
// period for sending inactive cursor messages
const inactiveCursorsPeriod = 500 * time.Millisecond
func New( func New(
sessions types.SessionManager, sessions types.SessionManager,
desktop types.DesktopManager, desktop types.DesktopManager,
@ -132,45 +135,50 @@ func (manager *WebSocketManagerCtx) Start() {
manager.fileChooserDialogEvents() manager.fileChooserDialogEvents()
manager.wg.Add(1) if manager.sessions.InactiveCursors() {
go func() { manager.logger.Info().Msg("starting inactive cursors handler")
defer manager.wg.Done()
ticker := time.NewTicker(500 * time.Millisecond) manager.wg.Add(1)
defer ticker.Stop() go func() {
defer manager.wg.Done()
lastEmpty := false ticker := time.NewTicker(inactiveCursorsPeriod)
defer ticker.Stop()
for { lastEmpty := false
select {
case <-manager.shutdown:
return
case <-ticker.C:
cursorsMap := manager.sessions.PopCursors()
length := len(cursorsMap) for {
if length == 0 && lastEmpty { select {
continue case <-manager.shutdown:
manager.logger.Info().Msg("stopping inactive cursors handler")
return
case <-ticker.C:
cursorsMap := manager.sessions.PopCursors()
length := len(cursorsMap)
if length == 0 && lastEmpty {
continue
}
lastEmpty = length == 0
cursors := []message.SessionCursor{}
for session, cursor := range cursorsMap {
cursors = append(
cursors,
message.SessionCursor{
ID: session.ID(),
X: uint16(cursor.X),
Y: uint16(cursor.Y),
},
)
}
// TODO: Send to subscribers only.
manager.sessions.AdminBroadcast(event.SESSION_CURSORS, cursors, nil)
} }
lastEmpty = length == 0
cursors := []message.SessionCursor{}
for session, cursor := range cursorsMap {
cursors = append(
cursors,
message.SessionCursor{
ID: session.ID(),
X: uint16(cursor.X),
Y: uint16(cursor.Y),
},
)
}
// TODO: Send to subscribers only.
manager.sessions.AdminBroadcast(event.SESSION_CURSORS, cursors, nil)
} }
} }()
}() }
manager.logger.Info().Msg("websocket starting") manager.logger.Info().Msg("websocket starting")
} }