diff --git a/client.go b/client.go index 14537db..f49b298 100644 --- a/client.go +++ b/client.go @@ -108,6 +108,9 @@ Setting up the client / Checking the parameters // If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID. // Default the port to 5222. func NewClient(config Config, r *Router) (c *Client, err error) { + if config.KeepaliveInterval == 0 { + config.KeepaliveInterval = time.Second * 30 + } // Parse JID if config.parsedJid, err = NewJid(config.Jid); err != nil { err = errors.New("missing jid") @@ -185,7 +188,7 @@ func (c *Client) Resume(state SMState) error { // Start the keepalive go routine keepaliveQuit := make(chan struct{}) - go keepalive(c.transport, keepaliveQuit) + go keepalive(c.transport, c.config.KeepaliveInterval, keepaliveQuit) // Start the receiver go routine state = c.Session.SMState // Leaving this channel here for later. Not used atm. We should return this instead of an error because right @@ -312,9 +315,8 @@ func (c *Client) recv(state SMState, keepaliveQuit chan<- struct{}, errChan chan // Loop: send whitespace keepalive to server // This is use to keep the connection open, but also to detect connection loss // and trigger proper client connection shutdown. -func keepalive(transport Transport, quit <-chan struct{}) { - // TODO: Make keepalive interval configurable - ticker := time.NewTicker(30 * time.Second) +func keepalive(transport Transport, interval time.Duration, quit <-chan struct{}) { + ticker := time.NewTicker(interval) for { select { case <-ticker.C: diff --git a/config.go b/config.go index e3ea108..da4d4ab 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package xmpp import ( "os" + "time" ) // Config & TransportConfiguration must not be modified after having been passed to NewClient. Any @@ -9,12 +10,13 @@ import ( type Config struct { TransportConfiguration - Jid string - parsedJid *Jid // For easier manipulation - Credential Credential - StreamLogger *os.File // Used for debugging - Lang string // TODO: should default to 'en' - ConnectTimeout int // Client timeout in seconds. Default to 15 + Jid string + parsedJid *Jid // For easier manipulation + Credential Credential + StreamLogger *os.File // Used for debugging + Lang string // TODO: should default to 'en' + KeepaliveInterval time.Duration // Interval between keepalive packets + ConnectTimeout int // Client timeout in seconds. Default to 15 // Insecure can be set to true to allow to open a session without TLS. If TLS // is supported on the server, we will still try to use it. Insecure bool