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

33 lines
800 B
Go
Raw Permalink Normal View History

2024-12-28 19:11:50 +01:00
package msghandlers
import (
"daemonSocketExample/daemonconn"
"log/slog"
"time"
2024-12-28 19:11:50 +01:00
)
func handleEchoRequest(ctx HandlerContext, msg daemonconn.Message) error {
2024-12-28 19:11:50 +01:00
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)
2024-12-28 19:11:50 +01:00
return nil
}