1
0
Fork 0

Implement XMPP closing.

This commit is contained in:
Matt Goodall 2012-07-18 16:30:44 +01:00
parent 703012bbb5
commit 8bb5c81347
2 changed files with 21 additions and 0 deletions

View File

@ -95,6 +95,18 @@ func (stream *Stream) SendStart(start *xml.StartElement) (*xml.StartElement, err
return rstart, nil return rstart, nil
} }
// Send the end element that closes the stream.
func (stream *Stream) SendEnd(end *xml.EndElement) error {
buf := new(bytes.Buffer)
if err := writeXMLEndElement(buf, end); err != nil {
return err
}
if err := stream.send(buf.Bytes()); err != nil {
return err
}
return nil
}
// Send a stanza. Used to write a complete, top-level element. // Send a stanza. Used to write a complete, top-level element.
func (stream *Stream) Send(v interface{}) error { func (stream *Stream) Send(v interface{}) error {
if stream.config.LogStanzas { if stream.config.LogStanzas {

View File

@ -1,6 +1,7 @@
package xmpp package xmpp
import ( import (
"encoding/xml"
"fmt" "fmt"
"log" "log"
"sync" "sync"
@ -8,6 +9,8 @@ import (
// Handles XMPP conversations over a Stream. Use NewClientXMPP or // Handles XMPP conversations over a Stream. Use NewClientXMPP or
// NewComponentXMPP to create and configure a XMPP instance. // NewComponentXMPP to create and configure a XMPP instance.
// Close the conversation by closing the Out channel, the In channel will be
// closed when the remote server closes its stream.
type XMPP struct { type XMPP struct {
// JID associated with the stream. Note: this may be negotiated with the // JID associated with the stream. Note: this may be negotiated with the
@ -158,9 +161,15 @@ func IqResult(id string) Matcher {
} }
func (x *XMPP) sender() { func (x *XMPP) sender() {
// Send outgoing elements to the stream until the channel is closed.
for v := range x.Out { for v := range x.Out {
x.stream.Send(v) x.stream.Send(v)
} }
// Close the stream. Note: relies on common element name for all types of
// XMPP connection.
x.stream.SendEnd(&xml.EndElement{xml.Name{"stream", "stream"}})
} }
func (x *XMPP) receiver() { func (x *XMPP) receiver() {