Annotate

doc/example.lua @ 433:5c77923ec1d9

docs/example.lua: Don't depend on 'verse' being a global
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Oct 2020 15:27:48 +0100
parent 260:7f6df45a3d1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
58
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Change these:
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local jid, password = "user@example.com", "secret";
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- This line squishes verse each time you run,
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- handy if you're hacking on Verse itself
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --os.execute("squish --minify-level=none verse");
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
433
5c77923ec1d9 docs/example.lua: Don't depend on 'verse' being a global
Matthew Wild <mwild1@gmail.com>
parents: 260
diff changeset
8 local verse = require "verse".init("client");
58
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
108
c67ed3fc5571 doc/example.lua: Don't add sasl plugin, since it is enabled by default
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
10 c = verse.new();
58
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 c:add_plugin("version");
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 -- Add some hooks for debugging
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 c:hook("opened", function () print("Stream opened!") end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 c:hook("closed", function () print("Stream closed!") end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 -- This one prints all received data
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 c:hook("incoming-raw", print, 1000);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 -- Print a message after authentication
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 c:hook("authentication-success", function () print("Logged in!"); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 -- Print a message and exit when disconnected
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 -- Now, actually start the connection:
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 c:connect_client(jid, password);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
77
7af3e57501c2 doc/example.lua: Update to use 'ready' event
Matthew Wild <mwild1@gmail.com>
parents: 58
diff changeset
31 -- Catch the "ready" event to know when the stream is ready to use
7af3e57501c2 doc/example.lua: Update to use 'ready' event
Matthew Wild <mwild1@gmail.com>
parents: 58
diff changeset
32 c:hook("ready", function ()
58
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 print("Stream ready!");
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 c.version:set{ name = "verse example client" };
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 c:query_version(c.jid, function (v) print("I am using "..(v.name or "<unknown>")); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 print("Starting loop...")
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 verse.loop()