-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
58 lines (50 loc) · 925 Bytes
/
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
package main
import (
"log"
"net/http"
"time"
"github.com/go-macaron/jade"
"github.com/gorilla/websocket"
"gopkg.in/macaron.v1"
)
var (
m = macaron.Classic()
upgrader = websocket.Upgrader{} // use default options
)
func init() {
m.Use(jade.Renderer())
}
func logws(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
for {
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
time.Sleep(time.Millisecond * 500)
}
}
func homepage(r jade.Render) {
r.HTML(200, "index", nil)
}
func main() {
m.Get("/", homepage)
m.Get("/ws", logws)
m.Run()
}