This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
67 lines (58 loc) · 1.7 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
package main
import (
"github.com/gorilla/websocket"
"net/http"
"log"
"fmt"
"encoding/base64"
"time"
"io/ioutil"
);
func main() {
http.HandleFunc("/connws/", ConnWs)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func ConnWs(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(w, "Not a websocket handshake", 400)
return
} else if err != nil {
log.Println(err)
return
}
var img64 [] byte
res := map[string]interface{}{}
for {
if err = ws.ReadJSON(&res); err != nil {
if err.Error() == "EOF" {
return
}
// ErrShortWrite means a write accepted fewer bytes than requested then failed to return an explicit error.
if err.Error() == "unexpected EOF" {
return
}
fmt.Println("Read : " + err.Error())
return
}
res["a"] = "a"
log.Println(res)
for {
files, _ := ioutil.ReadDir("./images");
for _, f := range files {
img64, _ = ioutil.ReadFile("./images/" + f.Name())
str := base64.StdEncoding.EncodeToString(img64)
res["img64"] = str
if err = ws.WriteJSON(&res); err != nil {
fmt.Println("watch dir - Write : " + err.Error())
//return
}
time.Sleep(50 * time.Millisecond);
}
time.Sleep(50 * time.Millisecond);
}
}
}