Archived
2
0

clipboard sync and some minor fixes

This commit is contained in:
Craig
2020-01-25 14:29:52 +00:00
parent e3a73aa264
commit 56a5dcf77f
22 changed files with 359 additions and 88 deletions

View File

@ -1,6 +1,7 @@
package websocket
import (
"n.eko.moe/neko/internal/hid/clipboard"
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/event"
"n.eko.moe/neko/internal/types/message"
@ -104,3 +105,14 @@ func (h *MessageHandler) controlGive(id string, session types.Session, payload *
return nil
}
func (h *MessageHandler) controlClipboard(id string, session types.Session, payload *message.Clipboard) error {
// check if session is host
if !h.sessions.IsHost(id) {
h.logger.Debug().Str("id", id).Msg("is not the host")
return nil
}
clipboard.WriteAll(payload.Text)
return nil
}

View File

@ -82,6 +82,12 @@ func (h *MessageHandler) Message(id string, raw []byte) error {
utils.Unmarshal(payload, raw, func() error {
return h.controlGive(id, session, payload)
}), "%s failed", header.Event)
case event.CONTROL_CLIPBOARD:
payload := &message.Clipboard{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.controlClipboard(id, session, payload)
}), "%s failed", header.Event)
// Chat Events
case event.CHAT_MESSAGE:

View File

@ -10,6 +10,7 @@ import (
"github.com/rs/zerolog/log"
"n.eko.moe/neko/internal/config"
"n.eko.moe/neko/internal/hid/clipboard"
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/event"
"n.eko.moe/neko/internal/types/message"
@ -51,20 +52,6 @@ type WebSocketHandler struct {
}
func (ws *WebSocketHandler) Start() error {
go func() {
defer func() {
ws.logger.Info().Msg("shutdown")
}()
for {
select {
case <-ws.shutdown:
return
}
}
}()
ws.sessions.OnCreated(func(id string, session types.Session) {
if err := ws.handler.SessionCreated(id, session); err != nil {
ws.logger.Warn().Str("id", id).Err(err).Msg("session created with and error")
@ -89,6 +76,40 @@ func (ws *WebSocketHandler) Start() error {
}
})
go func() {
defer func() {
ws.logger.Info().Msg("shutdown")
}()
current := ""
clip, err := clipboard.ReadAll()
if err == nil && clip != current {
current = clip
}
for {
select {
case <-ws.shutdown:
return
default:
if ws.sessions.HasHost() {
clip, err := clipboard.ReadAll()
if err == nil && clip != current {
session, ok := ws.sessions.GetHost()
if ok {
session.Send(message.Clipboard{
Event: event.CONTROL_CLIPBOARD,
Text: clip,
})
}
current = clip
}
}
time.Sleep(100 * time.Millisecond)
}
}
}()
return nil
}