├── README.md ├── client └── main.go └── server └── main.go /README.md: -------------------------------------------------------------------------------- 1 | # GoVnc 2 | golang版 屏幕监控 3 | 4 | 需要安装第三方包 5 | `go get github.com/saljam/mjpeg ` 6 | `go get github.com/vova616/screenshot` 7 | 8 | 本机环境需要装有 mingw32 9 | 10 | 视频教程地址:https://www.bilibili.com/video/BV1RP411u72u/ 11 | 12 | 13 | -------------------------------------------------------------------------------- /client/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "image/jpeg" 7 | "io/ioutil" 8 | "net/http" 9 | "os" 10 | 11 | "github.com/saljam/mjpeg" 12 | "github.com/vova616/screenshot" 13 | ) 14 | 15 | var ( 16 | logo = ` 17 | \__ | | | |__ _____|__|_ _ __ ____ |__| 18 | / | | | | \ / ___/ \ \/ \/ // __ \| | 19 | \____ | | Y \\___ \| |\ /\ ___/| | 20 | / ______|___|___| /____ >__| \/\_/ \___ >__| 21 | \/ \/ \/ \/ 22 | ` 23 | tvb = "这是我的频道欢迎投稿学习:https://space.bilibili.com/353948151 " 24 | 25 | keytishi = ` 26 | 首先编译好命令参数如 : client.exe ip port 27 | 例 如 :client.exe 127.0.0.1 9527 28 | ` 29 | ) 30 | 31 | func main() { 32 | fmt.Println(logo) 33 | fmt.Println(tvb) 34 | if len(os.Args) != 3 { 35 | fmt.Println(keytishi) 36 | return 37 | } 38 | ip := os.Args[1] 39 | port := os.Args[2] 40 | stream := mjpeg.NewStream() 41 | func(stream *mjpeg.Stream) { 42 | for { 43 | img, err := screenshot.CaptureScreen() 44 | if err != nil { 45 | continue 46 | } 47 | file, err := os.Create("./tmp.jpeg") 48 | if err != nil { 49 | continue 50 | } 51 | defer file.Close() 52 | jpeg.Encode(file, img, nil) 53 | buf, err := ioutil.ReadFile("tmp.jpeg") 54 | if err != nil { 55 | continue 56 | } 57 | resp, err := http.Post("http://"+ip+":"+port+"/update", "multipart/form-data", bytes.NewReader(buf)) 58 | if err != nil { 59 | break 60 | } 61 | resp.Body.Close() 62 | stream.UpdateJPEG(buf) 63 | } 64 | }(stream) 65 | 66 | } 67 | -------------------------------------------------------------------------------- /server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | "net/http" 8 | "os" 9 | 10 | "github.com/saljam/mjpeg" 11 | ) 12 | 13 | var ( 14 | stream = mjpeg.NewStream() 15 | ) 16 | 17 | //服务端编写的业务逻辑处理程序 —— 回调函数 18 | func myHandler(w http.ResponseWriter, r *http.Request) { 19 | //fmt.Println("method = ", r.Method) //请求方法 20 | //fmt.Println("URL = ", r.URL) // 浏览器发送请求文件路径 21 | //fmt.Println("header = ", r.Header) // 请求头 22 | //fmt.Println("body = ", r.Body) // 请求包体 23 | //fmt.Println(r.RemoteAddr, "连接成功") //客户端网络地址 24 | 25 | //len := r.ContentLength 26 | //fmt.Println(len) 27 | bodys, _ := ioutil.ReadAll(r.Body) 28 | ////fmt.Println(body) 29 | 30 | stream.UpdateJPEG(bodys) 31 | //f, _ := os.Create("1.jpeg") 32 | //_, _ = f.Write(bodys) 33 | //f.Close() 34 | } 35 | 36 | var ( 37 | logo = ` 38 | \__ | | | |__ _____|__|_ _ __ ____ |__| 39 | / | | | | \ / ___/ \ \/ \/ // __ \| | 40 | \____ | | Y \\___ \| |\ /\ ___/| | 41 | / ______|___|___| /____ >__| \/\_/ \___ >__| 42 | \/ \/ \/ \/ 43 | ` 44 | tvb = "这是我的频道欢迎投稿学习:https://space.bilibili.com/353948151 " 45 | 46 | keytishi = ` 47 | 首先编译好命令参数如 : server.exe port 48 | 例 如 :server.exe 9527 49 | ` 50 | ) 51 | 52 | func main() { 53 | fmt.Println(logo) 54 | fmt.Println(tvb) 55 | if len(os.Args) != 2 { 56 | fmt.Println(keytishi) 57 | return 58 | } 59 | port := os.Args[1] 60 | http.HandleFunc("/update", myHandler) // 注册处理函数 61 | http.HandleFunc("/mjpeg", stream.ServeHTTP) 62 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 63 | content := "" 64 | w.Header().Add("Content-Type", "text/html;charset=utf-8") 65 | w.Write([]byte(content)) 66 | }) 67 | 68 | log.Fatal(http.ListenAndServe("0.0.0.0:"+port, nil)) 69 | } 70 | --------------------------------------------------------------------------------