Add SendIQ to StreamClient and Sender

This makes it possible to use SendIQ from PostConnect and route handlers.
This commit is contained in:
Wichert Akkerman 2019-10-31 20:08:39 +01:00 committed by Mickaël Rémond
parent a0e74051fd
commit eda5c23c54
2 changed files with 23 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package xmpp
import (
"context"
"crypto/sha1"
"encoding/hex"
"encoding/xml"
@ -158,6 +159,25 @@ func (c *Component) Send(packet stanza.Packet) error {
return nil
}
// SendIQ sends an IQ set or get stanza to the server. If a result is received
// the provided handler function will automatically be called.
//
// The provided context should have a timeout to prevent the client from waiting
// forever for an IQ result. For example:
//
// ctx, _ := context.WithTimeout(context.Background(), 30 * time.Second)
// result := <- client.SendIQ(ctx, iq)
//
func (c *Component) SendIQ(ctx context.Context, iq stanza.IQ) (chan stanza.IQ, error) {
if iq.Attrs.Type != "set" && iq.Attrs.Type != "get" {
return nil, ErrCanOnlySendGetOrSetIq
}
if err := c.Send(iq); err != nil {
return nil, err
}
return c.router.NewIQResultRoute(ctx, iq.Attrs.Id), nil
}
// SendRaw sends an XMPP stanza as a string to the server.
// It can be invalid XML or XMPP content. In that case, the server will
// disconnect the component. It is up to the user of this method to

View file

@ -1,6 +1,7 @@
package xmpp
import (
"context"
"errors"
"sync"
"time"
@ -26,6 +27,7 @@ type StreamClient interface {
Connect() error
Resume(state SMState) error
Send(packet stanza.Packet) error
SendIQ(ctx context.Context, iq stanza.IQ) (chan stanza.IQ, error)
SendRaw(packet string) error
Disconnect()
SetHandler(handler EventHandler)
@ -35,6 +37,7 @@ type StreamClient interface {
// It is mostly use in callback to pass a limited subset of the stream client interface
type Sender interface {
Send(packet stanza.Packet) error
SendIQ(ctx context.Context, iq stanza.IQ) (chan stanza.IQ, error)
SendRaw(packet string) error
}