Annotate

doc/example_pubsub.lua @ 391:b496f0262a3f

libs.encodings: mime.unb64 is buggy with \0 should have been fixed
author Kim Alvefur <zash@zash.se>
date Tue, 25 Aug 2015 16:27:38 +0200
parent 270:6a9a99aa062b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
260
7f6df45a3d1f doc/example*.lua: Update to use new .init() method
Matthew Wild <mwild1@gmail.com>
parents: 230
diff changeset
8 require "verse".init("client");
230
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 c = verse.new();
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 c:add_plugin("pubsub");
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 -- Add some hooks for debugging
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 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
15 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
16 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
17
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 -- This one prints all received data
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 c:hook("incoming-raw", print, 1000);
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 -- Print a message after authentication
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 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
23 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
24
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 -- Print a message and exit when disconnected
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 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
27
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 -- Now, actually start the connection:
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 c:connect_client(jid, password);
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 -- 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
32 c:hook("ready", function ()
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 print("Stream ready!");
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- Create a reference to a node
270
6a9a99aa062b doc/example_pubsub.lua: Fix typo.
Kim Alvefur <zash@zash.se>
parents: 260
diff changeset
36 local node = c.pubsub("pubsub.shakespeare.lit", "princely_musings");
230
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 -- 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
39 node:hook(function(event)
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 print(event.item)
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end);
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 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
43
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 node:publish(
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 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
46 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
47 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
48 :tag("foobar"),
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 function(success) -- callback
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 print("publish", success and "success" or "failure")
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end)
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
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 print("Starting loop...")
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 verse.loop()
44a6da432e7e doc/example_pubsub.lua: Example plugins.pubsub usage
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56