Software /
code /
verse
Comparison
client.lua @ 13:c3d83b98fb4f
verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 28 Nov 2009 22:30:25 +0000 |
parent | 12:73f466054ead |
child | 21:00da62000b83 |
comparison
equal
deleted
inserted
replaced
12:73f466054ead | 13:c3d83b98fb4f |
---|---|
52 self.jid, self.password = jid, pass; | 52 self.jid, self.password = jid, pass; |
53 self.username, self.host, self.resource = jid_split(jid); | 53 self.username, self.host, self.resource = jid_split(jid); |
54 | 54 |
55 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); | 55 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); |
56 | 56 |
57 self.curr_id = 0; | |
58 | |
59 self.tracked_iqs = {}; | |
60 self:hook("stanza", function (stanza) | |
61 local id, type = stanza.attr.id, stanza.attr.type; | |
62 if id and stanza.name == "iq" and (type == "result" or type == "error") and self.tracked_iqs[id] then | |
63 self.tracked_iqs[id](stanza); | |
64 self.tracked_iqs[id] = nil; | |
65 return true; | |
66 end | |
67 end); | |
68 | |
57 -- Initialise connection | 69 -- Initialise connection |
58 self:connect(self.connect_host or self.host, self.connect_port or 5222); | 70 self:connect(self.connect_host or self.host, self.connect_port or 5222); |
59 --reset_stream(self); | 71 --reset_stream(self); |
60 self:reopen(); | 72 self:reopen(); |
61 end | 73 end |
70 self:send("</stream:stream>"); | 82 self:send("</stream:stream>"); |
71 end | 83 end |
72 self.conn:close(); | 84 self.conn:close(); |
73 end | 85 end |
74 | 86 |
87 function stream:send_iq(iq, callback) | |
88 local id = self:new_id(); | |
89 self.tracked_iqs[id] = callback; | |
90 iq.attr.id = id; | |
91 self:send(iq); | |
92 end | |
93 | |
94 function stream:new_id() | |
95 self.curr_id = self.curr_id + 1; | |
96 return tostring(self.curr_id); | |
97 end |