-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (63 loc) · 1.45 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
68
69
70
71
72
73
74
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func reader(conn *websocket.Conn) {
go func() {
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
log.Printf("Error on reading the message %v", err)
return
}
fmt.Printf("WS Message: %s", p)
if err := conn.WriteMessage(messageType, p); err != nil {
log.Printf("Error on responding to message %v", err)
return
}
}
}()
go func() {
for {
err := conn.WriteMessage(websocket.TextMessage, []byte("^Ping\n"))
if err != nil {
log.Printf("Error on sending the message %v", err)
}
fmt.Print("Message sent")
time.Sleep(5 * time.Second)
}
}()
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Error on upgrading the socket %v", err)
return
}
fmt.Println("Websocket connected")
reader(ws)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(time.Now().Second())
fmt.Println(time.Now().Second()%5 == 0)
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("OK"))
}
func setupRoutes() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/ws", wsHandler)
}
func main() {
setupRoutes()
log.Fatal(http.ListenAndServe(":8080", nil))
}