doc/example_component.lua

Sat, 21 Aug 2010 14:51:36 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 21 Aug 2010 14:51:36 +0100
changeset 99
0f5a8d530fcd
parent 86
508f653e9d46
child 151
75016d851648
permissions
-rw-r--r--

verse.plugins.disco: Add disco plugin originally developed by Hubert Chathi for Riddim, but here adapted for Verse with new APIs added to allow disco'ing the local server and remote entities

86
508f653e9d46 example_component.lua: Fix password
Matthew Wild <mwild1@gmail.com>
parents: 85
diff changeset
1 local jid, password = "echo.localhost", "hellohello";
85
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 --os.execute("squish --minify-level=none verse");
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 require "verse"
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 require "verse.component"
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 require "socket"
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 c = verse.new(verse.logger())
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 c:add_plugin("version");
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 c:add_plugin("ping");
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 -- Add some hooks for debugging
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 c:hook("opened", function () print("Stream opened!") end);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 c:hook("closed", function () print("Stream closed!") end);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 -- This one prints all received data
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 c:hook("incoming-raw", print, 1000);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 -- Print a message after authentication
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 c:hook("authentication-success", function () print("Logged in!"); end);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- Print a message and exit when disconnected
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Now, actually start the connection:
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 c.connect_host = "127.0.0.1"
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 c:connect_component(jid, password);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- Catch binding-success which is (currently) how you know when a stream is ready
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 c:hook("ready", function ()
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 print("Stream ready!");
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 c.version:set{ name = "verse example component" };
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end);
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 print("Starting loop...")
b9e71517d6e0 docs/example_component.lua: Example for connecting to the server as a component
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 verse.loop()

mercurial