package main import ( "encoding/json" "fmt" "os" "runtime" "time" "github.com/micmonay/keybd_event" "github.com/r3labs/sse/v2" ) func main() { deviceId := os.Getenv("DEVICE_ID") if deviceId == "" { println("DEVICE_ID env is not set") os.Exit(1) } // pushNotification("连接", "开始连接。") for { fmt.Println("开始连接。") // pushNotification("Connected", "Connected") client := sse.NewClient("https://ivampiresp.com/wp-content/rCMD/sse.php?device=" + deviceId) err := client.Subscribe("message", func(msg *sse.Event) { //fmt.Println(msg.Data) // byte to json var data map[string]interface{} err := json.Unmarshal(msg.Data, &data) //fmt.Println(data) if err != nil { fmt.Println(err) return } if data["cmd"] == nil || data["cmd"] == "" { //fmt.Println("cmd or params is nil") return } // interface to string command := fmt.Sprintf("%v", data["cmd"]) param := fmt.Sprintf("%v", data["param"]) if command != "press" { fmt.Println("此程序只作为演示工具使用,只处理 press 情况,但是意外的接收到了 " + command + ", 所以将不会处理。") return } fmt.Println("按下: " + param) if param == "up" { pressKey(keybd_event.VK_UP) } if param == "down" { pressKey(keybd_event.VK_DOWN) } if param == "left" { pressKey(keybd_event.VK_LEFT) } if param == "right" { pressKey(keybd_event.VK_RIGHT) } }) if err != nil { fmt.Println(err) // 下个循环重新连接 continue } } } func pressKey(key int) { kb, err := keybd_event.NewKeyBonding() if err != nil { panic(err) } // For linux, it is very important to wait 2 seconds if runtime.GOOS == "linux" { time.Sleep(2 * time.Second) } // Select keys to be pressed kb.SetKeys(key) // Set shift to be pressed kb.HasSHIFT(false) // Press the selected keys err = kb.Launching() if err != nil { panic(err) } // // Or you can use Press and Release // kb.Press() // time.Sleep(1 * time.Millisecond) // kb.Release() } // func pushNotification(title string, message string) { // //notification := toast.Notification{ // // AppID: "手势操作", // // Title: title, // // Message: message, // // //Icon: "go.png" // // //Actions: []toast.Action{ // // // {"protocol", "I'm a button", ""}, // // // {"protocol", "Me too!", ""}, // // //}, // //} // notification := toast.Notification{ // AppID: "ExampleApp", // Title: title, // Message: "Some message about how important something is...", // //Icon: "C:\\project\\notification\\notification\\assets\\icon.png", // //Actions: []toast.Action{ // // {Type: "protocol", Label: "I'm a button", Arguments: ""}, // // {Type: "protocol", Label: "Me too!", Arguments: ""}, // //}, // } // err := notification.Push() // if err != nil { // log.Fatalln(err) // } // }