-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_runner.go
56 lines (49 loc) · 1.13 KB
/
client_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"context"
"golang.design/x/clipboard"
"log"
"net"
)
func ClientRunner() {
conn, err := net.Dial("tcp", config.Host+":"+config.Port)
if err != nil {
return
}
pasteConnection := NewPasteConnection(conn)
defer conn.Close()
err = clipboard.Init()
if err != nil {
return
}
clipboardWatch := clipboard.Watch(context.Background(), clipboard.FmtText)
lastContent := clipboard.Read(clipboard.FmtText)
connReadCh := make(chan []byte, 1)
go func() {
for {
content, err := pasteConnection.Read()
if err != nil {
return
}
connReadCh <- content
}
}()
for {
select {
case content := <-clipboardWatch:
log.Printf("client clipboardWatch write: %s\nlastContent: %s\n", string(content), string(lastContent))
if string(lastContent) == string(content) {
continue
}
lastContent = content
pasteConnection.Send(content)
case content := <-connReadCh:
log.Printf("client received: %s\nlastContent: %s\n", string(content), string(lastContent))
if string(lastContent) == string(content) {
continue
}
clipboard.Write(clipboard.FmtText, content)
lastContent = content
}
}
}