daemon-socket-go-example/msghandlers/echorequesthandler.go

33 lines
800 B
Go

package msghandlers
import (
"daemonSocketExample/daemonconn"
"log/slog"
"time"
)
func handleEchoRequest(ctx HandlerContext, msg daemonconn.Message) error {
echoRequest := msg.(daemonconn.EchoRequest)
slog.Info("Got echo request", "echoByte", echoRequest.EchoByte, "updatesCount", echoRequest.UpdatesCount)
for i := 0; i < int(echoRequest.UpdatesCount); i++ {
err := ctx.conn.WriteMessage(daemonconn.EchoUpdate{EchoByte: echoRequest.EchoByte})
if err != nil {
return err
}
slog.Info("Sent echo update", "i", i)
time.Sleep(1 * time.Second)
}
echoResponse := daemonconn.EchoResponse{EchoByte: echoRequest.EchoByte}
err := ctx.conn.WriteMessage(echoResponse)
if err != nil {
return err
}
slog.Info("Sent echo response", "echoByte", echoResponse.EchoByte)
return nil
}