From 73f8698ad7617c0f4e7028a55088b92de70e534d Mon Sep 17 00:00:00 2001 From: "nedim.uka" Date: Fri, 17 Sep 2021 17:35:08 +0200 Subject: [PATCH] Added delay for client status ok message --- config/config.go | 1 + config/models.go | 1 + main.go | 1 - services/xmppsService.go | 10 +++++++--- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 492f417..4af88c3 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ func Load() { }, GeneralOptions: GeneralOptions{ DelayBetweenMassages: int64(getEnvInt("MASSAGE_DELAY", 120000000000)), + StatusMessageDelay: getEnvInt("STATUS_MASSAGE_DELAY", 10), }, } } diff --git a/config/models.go b/config/models.go index 0bf756c..74c6d9e 100644 --- a/config/models.go +++ b/config/models.go @@ -22,4 +22,5 @@ type Credentials struct { // GeneralOptions contains information for general configuration options type GeneralOptions struct { DelayBetweenMassages int64 + StatusMessageDelay int } diff --git a/main.go b/main.go index b9cea87..b6b89d0 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,6 @@ func main() { // Send online presence stanza in go rutines go func(xmppClient xmppService.XMPPClient) { - log.Printf("Online presence stanza FOR %v", xmppClient.Config.Jid) err := xmppService.SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid) if err != nil { diff --git a/services/xmppsService.go b/services/xmppsService.go index 23b176c..bf3358f 100644 --- a/services/xmppsService.go +++ b/services/xmppsService.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "log" + "math/rand" "os" "strconv" "time" @@ -132,8 +133,8 @@ func Init() { }() } - // Delay For one seccond to allow all clients to connect - time.Sleep(time.Duration(1000000000)) + // Delay For two seccond to allow all clients to connect + time.Sleep(time.Duration(2000000000)) } @@ -188,8 +189,11 @@ func handleMessage(s xmpp.Sender, p stanza.Packet) { } log.Printf("<%v> received following message: %v", msg.To, msg.Body) + statusMesageDelay := int64(rand.Intn(appConfig.AppConfig.GeneralOptions.StatusMessageDelay)) + log.Printf("DELAYING FOR: %v", statusMesageDelay*1000000000) + // Delay For two seccond to allow all clients to connect + time.Sleep(time.Duration(statusMesageDelay * 1000000000)) - // _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From) reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: `{"status": "OK"}`} err := s.Send(reply)