ping/pong

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

View File

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