Add xmpp ping periodically in order to keep connection alive.

This commit is contained in:
Chteufleur 2017-01-14 10:43:00 +01:00
parent aa1259b3dc
commit 8378253574
4 changed files with 59 additions and 37 deletions

View File

@ -2,10 +2,12 @@ appname = DataHouse
httpport = 8080 httpport = 8080
runmode = dev runmode = dev
mysqluser = "root" database = mysql
mysqlpass = "toto" mysqluser = "datahouse_user"
mysqlpass = "datahouse_user_password"
mysqlurls = "127.0.0.1" mysqlurls = "127.0.0.1"
mysqldb = "orm_test" mysqldb = "datahouse"
row_limit = 100000
sessionon = true sessionon = true
SessionProvider = memory SessionProvider = memory
@ -17,4 +19,5 @@ SessionCookieLifeTime = 3600
JID = test@kingpenguin.tk JID = test@kingpenguin.tk
PWD = test PWD = test
PORT = 5222 PORT = 5222
XMPP_DEBUG = false TIMER_PING = 60
XMPP_DEBUG = true

View File

@ -62,7 +62,7 @@ func main() {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
models.ChanRuns <- watchlog.EndRun models.ChanRuns <- watchlog.EndRun
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
models.ChanRuns <- xmpp_manager.EndRun go xmpp_manager.Run()
beego.Run() beego.Run()
} }
@ -72,10 +72,6 @@ func reborn() {
action := <-models.ChanRuns action := <-models.ChanRuns
switch action { switch action {
case xmpp_manager.EndRun:
go xmpp_manager.Run()
log.Info("XMPP Started")
case watchlog.EndRun: case watchlog.EndRun:
go watchlog.Run() go watchlog.Run()
log.Info("Watchlog Started") log.Info("Watchlog Started")

View File

@ -1,10 +1,12 @@
package xmpp_manager package xmpp_manager
import ( import (
"strconv"
"time"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/relay" "git.kingpenguin.tk/chteufleur/datahouse.git/models/relay"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables" "git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp" "git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
@ -26,8 +28,6 @@ const (
Type_unsubscribed = "unsubscribed" Type_unsubscribed = "unsubscribed"
Type_probe = "probe" Type_probe = "probe"
Type_error = "error" Type_error = "error"
EndRun = "EndRunXMPP"
) )
var ( var (
@ -41,12 +41,20 @@ var (
stream = new(xmpp.Stream) stream = new(xmpp.Stream)
client = new(xmpp.XMPP) client = new(xmpp.XMPP)
ping = false
pingId int64 = 0
timerDoPing, _ = beego.AppConfig.Int("TIMER_PING")
Debug = false Debug = false
) )
func init() { func init() {
log.SetLogger(variables.LogType, variables.LogParams) log.SetLogger(variables.LogType, variables.LogParams)
Debug = beego.AppConfig.String("XMPP_DEBUG") == "true" Debug = beego.AppConfig.String("XMPP_DEBUG") == "true"
if timerDoPing < 30 {
timerDoPing = 30
}
go doPing()
} }
func must(v interface{}, err error) interface{} { func must(v interface{}, err error) interface{} {
@ -59,6 +67,7 @@ func must(v interface{}, err error) interface{} {
func Run() { func Run() {
// Create stream and configure it as a component connection. // Create stream and configure it as a component connection.
log.Info("XMPP Run()") log.Info("XMPP Run()")
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID) jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
addrs, err := xmpp.HomeServerAddrs(jid) addrs, err := xmpp.HomeServerAddrs(jid)
if err == nil && len(addrs) > 0 { if err == nil && len(addrs) > 0 {
@ -66,13 +75,19 @@ func Run() {
client = must(xmpp.NewClientXMPP(stream, jid, passwdStr, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP) client = must(xmpp.NewClientXMPP(stream, jid, passwdStr, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
mainXMPP() mainXMPP()
time.Sleep(1 * time.Second)
} }
log.Debug("xmpp.Run Ended") log.Debug("xmpp.Run Ended")
models.ChanRuns <- EndRun go Run()
} }
func mainXMPP() { func mainXMPP() {
SendPresence(Status_online, Type_available, "") defer func() {
ping = false
log.Debug("mainXMPP Ended")
}()
SendPresence(Status_online, Type_available, "DataHouse")
ping = true
for x := range client.In { for x := range client.In {
switch v := x.(type) { switch v := x.(type) {
@ -90,14 +105,22 @@ func mainXMPP() {
log.Info("recv: %v", x) log.Info("recv: %v", x)
} }
} }
}
// Send deconnexion func doPing() {
SendPresence(Status_offline, Type_unavailable, "") for {
log.Debug("mainXMPP Ended") if ping {
iq := &xmpp.Iq{Id: strconv.FormatInt(pingId, 10), Type: xmpp.IQTypeGet, To: jid.Domain, From: jid.Full()}
iq.PayloadEncode(&xmpp.Ping{})
client.Out <- iq
pingId++
}
time.Sleep(time.Duration(timerDoPing) * time.Second)
}
} }
func SendPresence(status, tpye, message string) { func SendPresence(status, tpye, message string) {
client.Out <- xmpp.Presence{From: jid.Domain, Show: status, Type: tpye, Status: message} client.Out <- xmpp.Presence{From: jid.Full(), Show: status, Type: tpye, Status: message}
} }
func SendMessage(to, subject, message string) { func SendMessage(to, subject, message string) {