From 9394b361bd58064580ba2a83dde50254e1b4dfed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 16 Sep 2022 00:01:15 +0200 Subject: [PATCH] xevent on clipboard updated. --- server/internal/websocket/websocket.go | 52 ++++++-------------------- 1 file changed, 12 insertions(+), 40 deletions(-) diff --git a/server/internal/websocket/websocket.go b/server/internal/websocket/websocket.go index 0da0160..bd940fc 100644 --- a/server/internal/websocket/websocket.go +++ b/server/internal/websocket/websocket.go @@ -175,47 +175,19 @@ func (ws *WebSocketHandler) Start() { } }) - ws.wg.Add(1) - go func() { - defer func() { - ws.logger.Info().Msg("shutdown") - ws.wg.Done() - }() - - current := ws.desktop.ReadClipboard() - - for { - select { - case <-ws.shutdown: - return - default: - time.Sleep(100 * time.Millisecond) - - if !ws.sessions.HasHost() { - continue - } - - text := ws.desktop.ReadClipboard() - if text == current { - continue - } - - session, ok := ws.sessions.GetHost() - if ok { - err := session.Send(message.Clipboard{ - Event: event.CONTROL_CLIPBOARD, - Text: text, - }) - - if err != nil { - ws.logger.Err(err).Msg("unable to synchronize clipboard") - } - } - - current = text - } + ws.desktop.OnClipboardUpdated(func() { + session, ok := ws.sessions.GetHost() + if !ok { + return } - }() + + err := session.Send(message.Clipboard{ + Event: event.CONTROL_CLIPBOARD, + Text: ws.desktop.ReadClipboard(), + }) + + ws.logger.Err(err).Msg("sync clipboard") + }) } func (ws *WebSocketHandler) Shutdown() error {