From 9be90df7bd51a8d0e74e57495dbf4f87a8b4c7e4 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Tue, 27 Jun 2017 00:33:21 +0200 Subject: [PATCH] ping/pong --- app/websockets/main_handler.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/websockets/main_handler.go b/app/websockets/main_handler.go index 632d3ae..3d69785 100644 --- a/app/websockets/main_handler.go +++ b/app/websockets/main_handler.go @@ -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"`