doc/example_bosh.lua

Wed, 27 Jun 2018 19:13:27 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 27 Jun 2018 19:13:27 +0100
changeset 419
bf2fe3fc2f73
parent 260
7f6df45a3d1f
permissions
-rw-r--r--

Makefile: Use configured squish path

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";
91
59d7141827be doc/example_bosh.lua: Put url into variable
Matthew Wild <mwild1@gmail.com>
parents: 88
diff changeset
3 local url = "http://example.com:80/http-bind";
88
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This line squishes verse each time you run,
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- handy if you're hacking on Verse itself
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --os.execute("squish --minify-level=none verse");
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
260
7f6df45a3d1f doc/example*.lua: Update to use new .init() method
Matthew Wild <mwild1@gmail.com>
parents: 91
diff changeset
9 require "verse".init("client", "bosh");
88
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
91
59d7141827be doc/example_bosh.lua: Put url into variable
Matthew Wild <mwild1@gmail.com>
parents: 88
diff changeset
11 c = verse.new_bosh(nil, url);
88
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 c:add_plugin("version");
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 -- Add some hooks for debugging
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 c:hook("opened", function () print("Stream opened!") end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 c:hook("closed", function () print("Stream closed!") end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 -- This one prints all received data
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 c:hook("incoming-raw", print, 1000);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 -- Print a message after authentication
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 c:hook("authentication-success", function () print("Logged in!"); end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 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
25
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Print a message and exit when disconnected
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- Now, actually start the connection:
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 c:connect_client(jid, password);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 -- 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
33 c:hook("ready", function ()
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 print("Stream ready!");
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 c.version:set{ name = "Verse example BOSH client" };
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 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
37 end);
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 print("Starting loop...")
e204ef45bdd6 Add doc/example_bosh.lua
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 verse.loop()

mercurial