-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (81 loc) · 1.86 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
package wsBroadcaster
import (
"fmt"
"github.com/gorilla/websocket"
"log"
"net/http"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type hub struct {
messages chan []byte
connections map[int]*websocket.Conn
addConn chan *websocket.Conn
}
// Initialize our main hub struct
var h = &hub{
messages: make(chan []byte),
connections: make(map[int]*websocket.Conn),
addConn: make(chan *websocket.Conn),
}
func (h *hub) sendAll(message []byte) {
expired := []int{}
for i, conn := range h.connections {
err := conn.WriteMessage(websocket.BinaryMessage, []byte(message))
if err != nil {
conn.Close()
expired = append(expired, i)
}
}
// Prune the obsolete connections
if len(expired) > 0 {
for _, connId := range expired {
log.Println("Closed connection:", connId)
delete(h.connections, connId)
}
}
}
func Broadcast(message []byte) {
h.messages <- []byte(message)
}
func (h *hub) Run() {
id := 0
for {
select {
// Client has connected
case c := <-h.addConn:
log.Println("WS client connected:", id)
h.connections[id] = c
id++
// A new message has been received
case c := <-h.messages:
h.sendAll([]byte(c))
}
}
}
// Handle upgrades to websocket
func connectionHandler(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
}
h.addConn <- c
}
func Start(portNo int, tls bool, certFile string, keyFile string) {
go h.Run()
http.HandleFunc("/", connectionHandler)
var err error
if tls == true {
log.Printf("Serving secure WS on %d...", portNo)
err = http.ListenAndServeTLS(fmt.Sprintf(":%d", portNo), certFile, keyFile, nil)
} else {
log.Printf("Serving WS on %d...", portNo)
err = http.ListenAndServe(fmt.Sprintf(":%d", portNo), nil)
}
log.Fatal(err)
}