我制作了一个网络应用程序来关闭我的电脑屏幕,有几种不同的技术,但它相当简单:
我有一个 html/js 前端,它检测按钮点击(屏幕开启/屏幕关闭),它通过 ajax 将选项发送到 PHP 后端
然后 php 通过 tcp 端口连接,将选项发送到用 golang 编写的程序
然后我的 golang 程序执行关闭/打开屏幕的命令。它运行的命令是 ("xset -display :0 dpms force off")
我遇到的问题是该命令仅在我在终端中运行 golang 程序时才有效,但是当我将其设置为服务时,该命令将不起作用。
这是golang代码:
package main
import (
"os/exec"
"net"
"fmt"
"bufio"
)
func main() {
fmt.Println("Launching server")
ln, _ := net.Listen("tcp", ":7777")
fmt.Println("Listening...\n")
for {
// accept connection on port
conn, _ := ln.Accept()
fmt.Println("New connection")
// listen for message ending in \n
message, _ := bufio.NewReader(conn).ReadString('\n')
rec := string(message)
// remove trailing \n
rec = rec[:len(rec)-1]
fmt.Println("Message Received: ", "\""+rec+"\"")
returnMessage := "fail"
if (rec == "screensOff") {
fmt.Println("Turning off screens...")
//execute screens off command
cmd := exec.Command("xset", "-display", ":0", "dpms", "force", "off")
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(stdout))
returnMessage = "done"
}
} else if (rec == "screensOn") {
fmt.Println("Turning on screens...");
//execute screens on command
cmd := exec.Command("xset", "-display", ":0", "dpms", "force", "on")
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(stdout))
returnMessage = "done"
}
returnMessage = "done"
}
conn.Write([]byte(returnMessage + "\n"))
conn.Close()
fmt.Println("Connection closed\n")
}
}
紫衣仙女
holdtom
相关分类