Software /
code /
verse
Annotate
doc/example_pubsub.lua @ 243:20842f98f9ff
doc/example_pep.lua: get_child_text()
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 24 Nov 2011 07:02:27 +0100 |
parent | 230:44a6da432e7e |
child | 260:7f6df45a3d1f |
rev | line source |
---|---|
230
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Change these: |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local jid, password = "user@example.com", "secret"; |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- This line squishes verse each time you run, |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- handy if you're hacking on Verse itself |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 --os.execute("squish --minify-level=none"); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 require "verse" -- Verse main library |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 require "verse.client" -- XMPP client library |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 c = verse.new(); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 c:add_plugin("pubsub"); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 -- Add some hooks for debugging |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 c:hook("opened", function () print("Stream opened!") end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 c:hook("closed", function () print("Stream closed!") end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 c:hook("stanza", function (stanza) print("Stanza:", stanza) end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 -- This one prints all received data |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 c:hook("incoming-raw", print, 1000); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 -- Print a message after authentication |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 c:hook("authentication-success", function () print("Logged in!"); end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 -- Print a message and exit when disconnected |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 -- Now, actually start the connection: |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 c:connect_client(jid, password); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 -- Catch the "ready" event to know when the stream is ready to use |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 c:hook("ready", function () |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 print("Stream ready!"); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 -- Create a reference to a node |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 local node = c:pubsub("pubsub.shakespeare.lit", "princely_musings"); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 -- Callback for when something is published to the node |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 node:hook(function(event) |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 print(event.item) |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 node:subscribe() -- so we actually get the notifications that above callback would get |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 node:publish( |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 nil, -- no id, so the service should give us one |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 nil, -- no options (not supported at the time of this writing) |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 verse.stanza("something", { xmlns = "http://example.com/pubsub-thingy" }) -- the actual payload, would turn up in event.item above |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 :tag("foobar"), |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 function(success) -- callback |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 print("publish", success and "success" or "failure") |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 end) |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 end); |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 print("Starting loop...") |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 verse.loop() |
44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |