From 097e8d2a87e903e2d12362f1dbe0f64a1df868b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 12 Nov 2021 15:27:05 +0000 Subject: [PATCH] Inactive cursors - multiple positions. --- internal/session/manager.go | 16 +++++++++++----- internal/types/message/messages.go | 7 +++---- internal/types/session.go | 6 +++--- internal/websocket/manager.go | 19 +++++++++---------- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/internal/session/manager.go b/internal/session/manager.go index f94e2131..492e5fbd 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -19,7 +19,7 @@ func New(config *config.Session) *SessionManagerCtx { config: config, tokens: make(map[string]string), sessions: make(map[string]*SessionCtx), - cursors: make(map[types.Session]types.Cursor), + cursors: make(map[types.Session][]types.Cursor), emmiter: events.New(), } @@ -56,7 +56,7 @@ type SessionManagerCtx struct { host types.Session hostMu sync.Mutex - cursors map[types.Session]types.Cursor + cursors map[types.Session][]types.Cursor cursorsMu sync.Mutex emmiter events.EventEmmiter @@ -205,15 +205,21 @@ func (manager *SessionManagerCtx) SetCursor(cursor types.Cursor, session types.S manager.cursorsMu.Lock() defer manager.cursorsMu.Unlock() - manager.cursors[session] = cursor + list, ok := manager.cursors[session] + if !ok { + list = []types.Cursor{} + } + + list = append(list, cursor) + manager.cursors[session] = list } -func (manager *SessionManagerCtx) PopCursors() map[types.Session]types.Cursor { +func (manager *SessionManagerCtx) PopCursors() map[types.Session][]types.Cursor { manager.cursorsMu.Lock() defer manager.cursorsMu.Unlock() cursors := manager.cursors - manager.cursors = make(map[types.Session]types.Cursor) + manager.cursors = make(map[types.Session][]types.Cursor) return cursors } diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index f6f00254..0365ea86 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -88,10 +88,9 @@ type SessionData struct { State types.SessionState `json:"state"` } -type SessionCursor struct { - ID string `json:"id"` - X uint16 `json:"x"` - Y uint16 `json:"y"` +type SessionCursors struct { + ID string `json:"id"` + Cursors []types.Cursor `json:"cursors"` } ///////////////////////////// diff --git a/internal/types/session.go b/internal/types/session.go index e8a6ed64..8bc0f3a4 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -13,8 +13,8 @@ var ( ) type Cursor struct { - X int - Y int + X int `json:"x"` + Y int `json:"y"` } type SessionState struct { @@ -56,7 +56,7 @@ type SessionManager interface { ClearHost() SetCursor(cursor Cursor, session Session) - PopCursors() map[Session]Cursor + PopCursors() map[Session][]Cursor Broadcast(event string, payload interface{}, exclude interface{}) AdminBroadcast(event string, payload interface{}, exclude interface{}) diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index a753d23a..f0aa2559 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -21,7 +21,7 @@ import ( const pingPeriod = 10 * time.Second // period for sending inactive cursor messages -const inactiveCursorsPeriod = 500 * time.Millisecond +const inactiveCursorsPeriod = 750 * time.Millisecond func New( sessions types.SessionManager, @@ -326,19 +326,18 @@ func (manager *WebSocketManagerCtx) inactiveCursors() { } lastEmpty = currentEmpty - cursors := []message.SessionCursor{} - for session, cursor := range cursorsMap { - cursors = append( - cursors, - message.SessionCursor{ - ID: session.ID(), - X: uint16(cursor.X), - Y: uint16(cursor.Y), + sessionCursors := []message.SessionCursors{} + for session, cursors := range cursorsMap { + sessionCursors = append( + sessionCursors, + message.SessionCursors{ + ID: session.ID(), + Cursors: cursors, }, ) } - manager.sessions.InactiveCursorsBroadcast(event.SESSION_CURSORS, cursors, nil) + manager.sessions.InactiveCursorsBroadcast(event.SESSION_CURSORS, sessionCursors, nil) } } }()