xmpp/layer3.go
changeset 144 9d7fdb1d2fc1
parent 143 62166e57800e
child 145 21a390dd3506
equal deleted inserted replaced
143:62166e57800e 144:9d7fdb1d2fc1
     8 	"crypto/tls"
     8 	"crypto/tls"
     9 	"time"
     9 	"time"
    10 )
    10 )
    11 
    11 
    12 // Callback to handle a stanza with a particular id.
    12 // Callback to handle a stanza with a particular id.
    13 type stanzaHandler struct {
    13 type callback struct {
    14 	id string
    14 	id string
    15 	// Return true means pass this to the application
    15 	// Return true means pass this to the application
    16 	f func(Stanza) bool
    16 	f func(Stanza) bool
    17 }
    17 }
    18 
    18 
   166 }
   166 }
   167 
   167 
   168 // Register a callback to handle the next XMPP stanza (iq, message, or
   168 // Register a callback to handle the next XMPP stanza (iq, message, or
   169 // presence) with a given id. The provided function will not be called
   169 // presence) with a given id. The provided function will not be called
   170 // more than once. If it returns false, the stanza will not be made
   170 // more than once. If it returns false, the stanza will not be made
   171 // available on the normal Client.In channel. The stanza handler
   171 // available on the normal Client.Recv channel. The callback must not
   172 // must not read from that channel, as deliveries on it cannot proceed
   172 // read from that channel, as deliveries on it cannot proceed until
   173 // until the handler returns true or false.
   173 // the handler returns true or false.
   174 func (cl *Client) HandleStanza(id string, f func(Stanza) bool) {
   174 func (cl *Client) SetCallback(id string, f func(Stanza) bool) {
   175 	h := &stanzaHandler{id: id, f: f}
   175 	h := &callback{id: id, f: f}
   176 	cl.handlers <- h
   176 	cl.handlers <- h
   177 }
   177 }
   178 
   178 
   179 // Send a request to bind a resource. RFC 3920, section 7.
   179 // Send a request to bind a resource. RFC 3920, section 7.
   180 func (cl *Client) bind(bindAdv *bindIq) {
   180 func (cl *Client) bind(bindAdv *bindIq) {
   218 		cl.Jid = *jid
   218 		cl.Jid = *jid
   219 		Info.Logf("Bound resource: %s", cl.Jid.String())
   219 		Info.Logf("Bound resource: %s", cl.Jid.String())
   220 		cl.bindDone()
   220 		cl.bindDone()
   221 		return false
   221 		return false
   222 	}
   222 	}
   223 	cl.HandleStanza(msg.Id, f)
   223 	cl.SetCallback(msg.Id, f)
   224 	cl.sendXml <- msg
   224 	cl.sendXml <- msg
   225 }
   225 }