doc/example_bosh.lua

Fri, 06 Aug 2010 16:31:30 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 06 Aug 2010 16:31:30 +0100
changeset 88
e204ef45bdd6
child 91
59d7141827be
permissions
-rw-r--r--

Add doc/example_bosh.lua

88
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Change these:
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local jid, password = "user@example.com", "secret";
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- This line squishes verse each time you run,
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- handy if you're hacking on Verse itself
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --os.execute("squish --minify-level=none verse");
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 require "verse" -- Verse main library
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 require "verse.bosh" -- Verse BOSH support
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 require "verse.client" -- XMPP client library
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 c = verse.new_bosh(nil, "http://example.com:5280/http-bind");
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 c:add_plugin("version");
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 -- Add some hooks for debugging
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 c:hook("opened", function () print("Stream opened!") end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 c:hook("closed", function () print("Stream closed!") end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 -- This one prints all received data
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 c:hook("incoming-raw", print, 1000);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- Print a message after authentication
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 c:hook("authentication-success", function () print("Logged in!"); end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 -- Print a message and exit when disconnected
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- Now, actually start the connection:
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 c:connect_client(jid, password);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- Catch the "ready" event to know when the stream is ready to use
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 c:hook("ready", function ()
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 print("Stream ready!");
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 c.version:set{ name = "Verse example BOSH client" };
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 c:query_version(c.jid, function (v) print("I am using "..(v.name or "<unknown>")); end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 print("Starting loop...")
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 verse.loop()

mercurial