-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (86 loc) · 2.32 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"log"
"time"
"github.com/go-vgo/robotgo"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
mw "github.com/labstack/echo/middleware"
"github.com/zserge/webview"
"golang.org/x/net/websocket"
)
// Message to receive/send
type Message struct {
Message string `json:"msg"`
}
func main() {
// channel to post window handles to
messages := make(chan string, 1)
// start the webserver
e := getServer(messages)
go e.Run(standard.New(":3000"))
// start the window handle listener
go startWindowListener(messages)
// start the mouse listener
go startMouseListener(messages)
// start the clipboard listener
go startClipboardListener(messages)
// start webview UI
webview.Open("RobotGo App", "http://localhost:3000", 400, 300, true)
}
func getServer(messages chan string) *echo.Echo {
// init the web server
e := echo.New()
// init app-wide middleware
e.Use(mw.Logger())
e.Use(mw.Recover())
e.Use(mw.Gzip())
// routes
e.Static("/", "public")
e.File("/", "public/index.html")
e.GET("/ws", standard.WrapHandler(websocket.Handler(func(ws *websocket.Conn) {
// loop forever receiving from channel
for {
msg := <-messages
log.Println("Received message: " + msg)
outgoing := new(Message)
outgoing.Message = msg
err := websocket.JSON.Send(ws, outgoing)
if err != nil {
log.Println("websocket send error")
log.Println(err.Error())
continue
}
}
})))
return e
}
func startWindowListener(messages chan string) {
// loop forever polling for a window title every second and post to channel
for {
time.Sleep(1 * time.Second)
title := robotgo.GetTitle()
log.Println("Sending window title: " + title)
messages <- "Window title: " + title
}
}
func startMouseListener(messages chan string) {
// loop forever polling for mouse position every second and post to channel
for {
time.Sleep(1 * time.Second)
x, y := robotgo.GetMousePos()
mpos := fmt.Sprintf("(%d, %d)", x, y)
log.Println("Sending mouse position: " + mpos)
messages <- "Mouse position: " + mpos
}
}
func startClipboardListener(messages chan string) {
// loop forever polling for clipboard every second and post to channel
for {
time.Sleep(1 * time.Second)
text, _ := robotgo.ReadAll()
log.Println("Sending clipboard: " + text)
messages <- "Clipboard: " + text
}
}