feed-to-muc/sendPings.go

59 lines
1.4 KiB
Go
Raw Normal View History

/* Copyright Martin Dosch
Licensed under the "MIT License" */
package main
import (
"log"
"strings"
"time"
"github.com/chilts/sid"
2024-01-11 15:54:06 +01:00
"github.com/xmppo/go-xmpp"
)
// Send a ping every 30 seconds after last successful ping to check if the MUC is still available.
func pingMUC(client *xmpp.Client, botJid string, muc string, mucNick string) {
var err error
for {
time.Sleep(30 * time.Second)
// Send ping to own MUC participant to check we are still joined.
ID, err = client.RawInformation(botJid, muc+"/"+mucNick, sid.Id(),
"get", "<ping xmlns='urn:xmpp:ping'/>")
if err != nil {
log.Fatal("Error: Can't send MUC self ping: ", err)
}
pingSent = time.Now()
pingReceived = false
// Check for result IQ as long as there was no reply yet.
for (time.Since(pingSent).Seconds() < 10.0) && !pingReceived {
time.Sleep(1 * time.Second)
if pingReceived {
break
}
}
// Quit if no ping reply was received.
if !pingReceived {
log.Fatal("MUC not available. pingMUC")
}
}
}
// Send a ping to the server every 30 seconds to check if the connection is still alive.
func pingServer(client *xmpp.Client, server string, botJid string) {
for {
time.Sleep(30 * time.Second)
// Send ping to server to check if connection still exists.
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
if err != nil {
log.Fatal("Error: Client2Server ping failed:", err)
}
}
}