-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
49 lines (42 loc) · 1.07 KB
/
net.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
package main
import (
"context"
"encoding/json"
"net/http"
"github.com/gorilla/websocket"
"go.uber.org/zap"
)
func getQueryParam(r *http.Request, name string) string {
params := r.URL.Query()
paramValues := params[name]
if len(paramValues) <= 0 {
return ""
}
return paramValues[0]
}
func readWebsocket(conn *websocket.Conn, cancel context.CancelFunc) {
for {
_, payload, err := conn.ReadMessage()
if err != nil {
zlog.Info("websocket received error (closing)", zap.Error(err))
conn.Close()
cancel()
return
}
zlog.Info("websocket received payload", zap.String("payload", string(payload)))
}
}
func maybeSendWebsocket(conn *websocket.Conn, objType string, obj interface{}) {
data, err := json.Marshal(map[string]interface{}{
"type": objType,
"payload": obj,
})
if err != nil {
zlog.Warn("cannot marshal object", zap.String("object_type", objType), zap.Reflect("object", obj))
return
}
if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
zlog.Info("cannot send data", zap.String("data", string(data)))
conn.Close()
}
}