Skip to content

Commit

Permalink
Respond to ping requests from websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 3, 2021
1 parent 36e565c commit c9736fe
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -132,6 +133,10 @@ func handleCommand(az *AppService, ws *websocket.Conn, msg *appservice.Websocket
}
}

type PingData struct {
Timestamp int64 `json:"timestamp"`
}

func actuallyHandleCommand(az *AppService, msg *appservice.WebsocketCommand) (resp interface{}, err error) {
defer func() {
panicErr := recover()
Expand All @@ -148,6 +153,17 @@ func actuallyHandleCommand(az *AppService, msg *appservice.WebsocketCommand) (re
if err != nil {
log.Println("Error forwarding", az.ID, "sync proxy start request:", err)
}
case "ping":
var req PingData
jsonErr := json.Unmarshal(msg.Data, &req)
now := time.Now()
if req.Timestamp > 0 {
pingStart := time.Unix(0, req.Timestamp * int64(time.Millisecond))
log.Printf("Received ping from %s in %s", az.ID, now.Sub(pingStart))
} else {
log.Printf("Received ping from %s with no timestamp (json error: %v)", az.ID, jsonErr)
}
resp = &PingData{now.UnixNano() / int64(time.Millisecond)}
default:
log.Printf("Unknown command %s in request #%d from websocket. Data: %s", msg.Command, msg.ReqID, msg.Data)
err = fmt.Errorf("unknown command %s", msg.Command)
Expand Down

0 comments on commit c9736fe

Please sign in to comment.