-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathchat.go
85 lines (71 loc) · 1.78 KB
/
chat.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
package main
import (
"encoding/json"
"io"
"net/http"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
// message sent to us by the javascript client
type message struct {
Handle string `json:"handle"`
Text string `json:"text"`
}
// validateMessage so that we know it's valid JSON and contains a Handle and
// Text
func validateMessage(data []byte) (message, error) {
var msg message
if err := json.Unmarshal(data, &msg); err != nil {
return msg, errors.Wrap(err, "Unmarshaling message")
}
if msg.Handle == "" && msg.Text == "" {
return msg, errors.New("Message has no Handle or Text")
}
return msg, nil
}
// handleWebsocket connection.
func handleWebsocket(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
m := "Unable to upgrade to websockets"
log.WithField("err", err).Println(m)
http.Error(w, m, http.StatusBadRequest)
return
}
rr.register(ws)
for {
mt, data, err := ws.ReadMessage()
l := log.WithFields(logrus.Fields{"mt": mt, "data": data, "err": err})
if err != nil {
if websocket.IsCloseError(err, websocket.CloseGoingAway) || err == io.EOF {
l.Info("Websocket closed!")
break
}
l.Error("Error reading websocket message")
}
switch mt {
case websocket.TextMessage:
msg, err := validateMessage(data)
if err != nil {
l.WithFields(logrus.Fields{"msg": msg, "err": err}).Error("Invalid Message")
break
}
rw.publish(data)
default:
l.Warning("Unknown Message!")
}
}
rr.deRegister(ws)
ws.WriteMessage(websocket.CloseMessage, []byte{})
}