doc/example_component.lua

1
local jid, password = "echo.localhost", "hellohello";
2
 
3
--os.execute("squish --minify-level=none verse");
4
 
5
require "verse".init("component");
6
 
7
c = verse.new(verse.logger())
8
c:add_plugin("version");
9
c:add_plugin("ping");
10
 
11
-- Add some hooks for debugging
12
c:hook("opened", function () print("Stream opened!") end);
13
c:hook("closed", function () print("Stream closed!") end);
14
c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
15
 
16
-- This one prints all received data
17
c:hook("incoming-raw", print, 1000);
18
 
19
-- Print a message after authentication
20
c:hook("authentication-success", function () print("Logged in!"); end);
21
c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end);
22
 
23
-- Print a message and exit when disconnected
24
c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
25
 
26
-- Now, actually start the connection:
27
c.connect_host = "127.0.0.1"
28
c:connect_component(jid, password);
29
 
30
-- Catch binding-success which is (currently) how you know when a stream is ready
31
c:hook("ready", function ()
32
	print("Stream ready!");
33
	c.version:set{ name = "verse example component" };
34
end);
35
 
36
-- Echo, echo, echo, echo...
37
c:hook("stanza", function (stanza)
38
	stanza.attr.from, stanza.attr.to = stanza.attr.to, stanza.attr.from;
39
	c:send(stanza);
40
end)
41
 
42
print("Starting loop...")
43
verse.loop()