-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
112 lines (92 loc) · 2.09 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"github.com/201RichK/memory_game/variable"
"html/template"
"log"
"net/http"
"strconv"
"strings"
"time"
)
func init() {
variable.TPL = template.Must(template.New("").Funcs(fm).ParseFiles("templates/index.html"))
}
func toView(i int) int {
return i+1
}
var fm = template.FuncMap{
"T" : toView,
}
func main() {
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./public"))))
http.HandleFunc("/", redirect)
http.HandleFunc("/memory", memory)
http.HandleFunc("/echo", echo)
fmt.Println("run at http://localhost:3002/memory")
log.Fatal(http.ListenAndServe(":3002", nil))
}
func memory(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "text/html")
_ = variable.TPL.ExecuteTemplate(w, "index.html", variable.Matrices[1])
}
func redirect(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
http.Redirect(w, r, "/memory", http.StatusSeeOther)
}
return
}
func echo (w http.ResponseWriter, r *http.Request) {
conn, _ := variable.Upgrader.Upgrade(w, r, nil)
for {
// Read message from browser
msgType, msg, err := conn.ReadMessage()
if err != nil {
return
}
if _ , err := strconv.Atoi(string(msg)); err == nil {
x, _ := strconv.Atoi(string(msg[0]))
t, _ := strconv.Atoi(string(msg[1]))
variable.IsSuccess(0, x, t, msgType, conn)
}
time.AfterFunc(3*time.Second, func() {
activeMatriceTbl := variable.ActiveMatrice(0)
time.Sleep(10*time.Millisecond)
msgTbl := strings.Join(activeMatriceTbl, " ")
if err = conn.WriteMessage(msgType, []byte(msgTbl)); err != nil {
return
}
time.AfterFunc(1*time.Second, func() {
_ = conn.WriteMessage(msgType, []byte("stop"))
})
})
if string(msg) == "ok" {
fmt.Println("start")
}
}
}
/**
package main
import (
"fmt"
"time"
)
func main() {
tick := time.Tick(1 * time.Second)
boom := time.After(20 * time.Second)
i := 20
for {
select {
case <-tick:
fmt.Println(i)
case <-boom:
fmt.Println("FIN !")
return
default:
i--
//fmt.Println(" .")
time.Sleep(1 * time.Second)
}
}
}
*/