xmpp : handling incoming messages asynchronous
I am trying to write an implementation of XMPP client (no开发者_开发问答 BOSH, direct TCP connection).
trying to figure out how to handle incoming messages in response to my own requests and to know which response relates to which request, most protocol tag the request with some unique id and the server sends that id with the response.
I have seen an ID parameters for IQ stanzas but they are reported to identify a session and not a unique message...
what is the best way to handle this issue ?
The ID attribute in IQ stanzas bind a request (get or set) to a response (result or error). For each request, a new ID should be used. That's the way to track responses for each request.
The new RFC 6120 has a good description of how IQs work. Make sure that you're matching both the sender and the id of the received stanza to ensure that someone else isn't guessing your id numbers. The typical pattern is to use a IQtracker, which you call like this (pseudo-code):
iq = create_xml_dom_that_is(<iq id='[counter++]' type='get' to='receiver'/>)
track(iq, callback)
where callback gets called when the we receive an iq stanza from the receiver with the expected id, or when some timespan has been reached without getting a response.
精彩评论