ping/pong

This commit is contained in:
Morgan Bazalgette 2017-06-27 00:33:21 +02:00
parent e766e951a5
commit 9be90df7bd

View File

@ -47,7 +47,9 @@ func handler(rawConn *websocket.Conn) {
c.WriteJSON(TypeInvalidMessage, "invalid message type")
continue
}
f(c, i)
if f != nil {
f(c, i)
}
}
}
@ -66,6 +68,7 @@ func (c *conn) WriteJSON(t string, data interface{}) error {
var messageHandler = map[string]func(c *conn, message incomingMessage){
TypeSubscribeScores: SubscribeScores,
TypePing: pingHandler,
}
// Server Message Types
@ -74,13 +77,19 @@ const (
TypeInvalidMessage = "invalid_message_type"
TypeSubscribedToScores = "subscribed_to_scores"
TypeNewScore = "new_score"
TypePong = "pong"
)
// Client Message Types
const (
TypeSubscribeScores = "subscribe_scores"
TypePing = "ping"
)
func pingHandler(c *conn, message incomingMessage) {
c.WriteJSON(TypePong, nil)
}
// Message is the wrapped information for a message sent to the client.
type Message struct {
Type string `json:"type"`