-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
72 lines (61 loc) · 1.42 KB
/
websocket.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-kit/log"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func newWebsocketHandler(ts Service, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logger.Log("msg", "error upgrading websocket", "err", err)
return
}
wsConn := &WSConnection{send: make(chan interface{}, 256), ws: conn}
ts.RegisterMeta(wsConn.send)
defer ts.UnregisterMeta(wsConn.send)
go wsConn.Writer()
wsConn.Reader()
}
}
type WSConnection struct {
// The websocket connection.
ws *websocket.Conn
// Buffered channel of outbound messages.
send chan interface{}
}
func (c *WSConnection) Reader() {
for {
if _, msg, err := c.ws.ReadMessage(); err == nil {
parsedMessage := map[string]interface{}{}
err = json.Unmarshal(msg, &parsedMessage)
if err != nil {
fmt.Printf("error decoding message json %v\n", err)
continue
}
} else {
break
}
}
c.ws.Close()
}
func (c *WSConnection) Writer() {
for message := range c.send {
bytes, err := json.Marshal(message)
if err != nil {
fmt.Printf("Failed to marshall notification to JSON, ignoring: %v\n", err)
continue
}
err = c.ws.WriteMessage(websocket.TextMessage, bytes)
if err != nil {
break
}
}
c.ws.Close()
}