File: xmpp_ping.go

package info (click to toggle)
golang-github-mattn-go-xmpp 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 220 kB
  • sloc: makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,479 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package xmpp

import (
	"fmt"
	"time"
)

func (c *Client) PingC2S(jid, server string) error {
	if jid == "" {
		jid = c.jid
	}
	if server == "" {
		server = c.domain
	}
	_, err := fmt.Fprintf(c.stanzaWriter, "<iq from='%s' to='%s' id='%s' type='get'>"+
		"<ping xmlns='urn:xmpp:ping'/>"+
		"</iq>\n",
		xmlEscape(jid), xmlEscape(server), getUUID())
	return err
}

func (c *Client) PingS2S(fromServer, toServer string) error {
	_, err := fmt.Fprintf(c.stanzaWriter, "<iq from='%s' to='%s' id='%s' type='get'>"+
		"<ping xmlns='urn:xmpp:ping'/>"+
		"</iq>\n",
		xmlEscape(fromServer), xmlEscape(toServer), getUUID())
	return err
}

func (c *Client) SendResultPing(id, toServer string) error {
	_, err := fmt.Fprintf(c.stanzaWriter, "<iq type='result' to='%s' id='%s'/>\n",
		xmlEscape(toServer), xmlEscape(id))
	return err
}

func (c *Client) sendPeriodicPings() {
	for range c.periodicPingTicker.C {
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		c.periodicPingID = getUUID()
		c.periodicPingReply = false
		_, err := fmt.Fprintf(c.stanzaWriter, "<iq from='%s' to='%s' id='%s' type='get'>"+
			"<ping xmlns='urn:xmpp:ping'/></iq>\n",
			xmlEscape(c.jid), xmlEscape(c.domain), c.periodicPingID)
		if err != nil {
			c.Close()
		}
		time.Sleep(c.periodicPingTimeout)
		if !c.periodicPingReply {
			c.shutdown = true
			fmt.Fprintf(c.stanzaWriter, "</stream:stream>\n")
			c.conn.Close()
		}
	}
}