2022-12-27 23:05:32 +00:00
|
|
|
# Streaming keyboard input
|
|
|
|
|
|
|
|
We use the Gorilla websocket library to handle the websocket connection. We then use the sendkeys library to stream the keyboard input to the client.
|
|
|
|
|
|
|
|
``` go
|
|
|
|
--- streaming keyboard input
|
|
|
|
|
|
|
|
func clientConnected(w http.ResponseWriter, r *http.Request) {
|
|
|
|
keyboard, err := sendkeys.NewKBWrapWithOptions(sendkeys.Noisy)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("upgrade:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
// get auth token
|
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if auth.CheckAuthToken(string(message)) != nil {
|
|
|
|
log.Println("invalid token")
|
2023-01-01 21:25:32 +00:00
|
|
|
c.WriteMessage(websocket.TextMessage, []byte("invalid token"))
|
2022-12-27 23:05:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.WriteMessage(websocket.TextMessage, []byte("authenticated"))
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(25 * time.Millisecond)
|
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Printf("recv: %s", message)
|
|
|
|
message_string := string(message)
|
2023-01-22 00:55:58 +00:00
|
|
|
if message_string == "" {
|
|
|
|
message_string = "\n"
|
|
|
|
}
|
2022-12-27 23:05:32 +00:00
|
|
|
err = keyboard.Type(message_string)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("type:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
---
|