diff --git a/internal/session/session.go b/internal/session/session.go index 8cf08892..5c84d726 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -16,6 +16,10 @@ type SessionCtx struct { profile types.MemberProfile state types.SessionState + positionX int + positionY int + positionMu sync.Mutex + websocketPeer types.WebSocketPeer websocketMu sync.Mutex @@ -53,6 +57,18 @@ func (session *SessionCtx) IsHost() bool { return session.manager.GetHost() == session } +// --- +// cursor position +// --- + +func (session *SessionCtx) SetPosition(x, y int) { + session.positionMu.Lock() + defer session.positionMu.Unlock() + + session.positionX = x + session.positionY = y +} + // --- // websocket // --- diff --git a/internal/types/session.go b/internal/types/session.go index 045daf84..c814fc5d 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -23,6 +23,9 @@ type Session interface { State() SessionState IsHost() bool + // cursor position + SetPosition(x, y int) + // websocket SetWebSocketPeer(websocketPeer WebSocketPeer) SetWebSocketConnected(websocketPeer WebSocketPeer, connected bool) diff --git a/internal/webrtc/handler.go b/internal/webrtc/handler.go index 898e4f47..3c06ae4f 100644 --- a/internal/webrtc/handler.go +++ b/internal/webrtc/handler.go @@ -34,15 +34,36 @@ func (manager *WebRTCManagerCtx) handle(data []byte, session types.Session) erro buffer = bytes.NewBuffer(data) - switch header.Event { - case payload.OP_MOVE: + // handle cursor move event + if header.Event == payload.OP_MOVE { payload := &payload.Move{} if err := binary.Read(buffer, binary.BigEndian, payload); err != nil { return err } - manager.desktop.Move(int(payload.X), int(payload.Y)) - manager.curPosition.Set(int(payload.X), int(payload.Y)) + x, y := int(payload.X), int(payload.Y) + + // handle active cursor movement + if session.IsHost() { + manager.desktop.Move(x, y) + manager.curPosition.Set(x, y) + return nil + } + + // handle inactive cursor movement + if session.Profile().CanHost { + session.SetPosition(x, y) + } + + return nil + } + + // continue only if session is host + if !session.IsHost() { + return nil + } + + switch header.Event { case payload.OP_SCROLL: payload := &payload.Scroll{} if err := binary.Read(buffer, binary.BigEndian, payload); err != nil { diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index 20407936..205f601e 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -208,10 +208,6 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin }) dataChannel.OnMessage(func(message webrtc.DataChannelMessage) { - if !session.IsHost() { - return - } - if err := manager.handle(message.Data, session); err != nil { logger.Err(err).Msg("data handle failed") }