-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (100 loc) · 2.59 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
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
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
"html/template"
"time"
)
var controlTempl = template.Must(template.ParseFiles("templates/control.html"))
var bigscreenTempl = template.Must(template.ParseFiles("templates/bigscreen.html"))
var clientTempl = template.Must(template.ParseFiles("templates/client.html"))
var upgrader = websocket.Upgrader{}
var sendBuffer = make(chan string, 64)
func bigscreenws(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
go writePump(c)
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, []byte("OK"))
if err != nil {
log.Println("write:", err)
break
}
}
}
func clientws(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
sendBuffer <- string(message[:])
}
}
// writePump pumps messages from the clients to the bigscreen connection.
func writePump(c *websocket.Conn) {
ticker := time.NewTicker(1 * time.Second)
defer func() {
ticker.Stop()
c.Close()
}()
for {
select {
case message, ok := <-sendBuffer:
if !ok {
c.WriteMessage(websocket.CloseMessage, []byte{})
return
}
if err := c.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
return
}
case <-ticker.C:
if err := c.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
return
}
}
}
}
func control(w http.ResponseWriter, req *http.Request) {
controlTempl.Execute(w, "ws://"+req.Host+"/clientws")
}
func bigscreen(w http.ResponseWriter, req *http.Request) {
bigscreenTempl.Execute(w, "ws://"+req.Host+"/bigscreenws")
}
func client(w http.ResponseWriter, req *http.Request) {
// The "/" pattern matches everything, so we need to check that we're at the root here
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
clientTempl.Execute(w, "ws://"+req.Host+"/clientws")
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/control", control)
http.HandleFunc("/bigscreenws", bigscreenws)
http.HandleFunc("/bigscreen", bigscreen)
http.HandleFunc("/clientws", clientws)
http.HandleFunc("/", client)
log.Fatal(http.ListenAndServe(":8080", nil))
log.Println("Listening on port 8080")
}