Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added hello world example

  • Loading branch information...
commit 130b94a14ecc5312fd05e06087a65fcfd278c55a 1 parent 3e7dd47
@crapooze authored
Showing with 63 additions and 0 deletions.
  1. +63 −0 samples/hello.rb
View
63 samples/hello.rb
@@ -0,0 +1,63 @@
+$LOAD_PATH.unshift './lib'
+require 'em-xmpp'
+require 'em-xmpp/helpers'
+
+if ARGV.empty?
+ puts "usage: #{__FILE__} <jid> [<pass>] [certificates-dir]"
+ exit 0
+end
+
+jid = ARGV.first
+pass = ARGV[1]
+certdir = ARGV[2]
+
+module RosterClient
+ attr_reader :roster
+
+ include EM::Xmpp::Helpers
+ def ready
+ puts "***** #{@jid} ready"
+
+ on_presence do |ctx|
+ if ctx.subscription_request?
+ puts "**** accepting subscription from #{ctx.from}"
+ send_stanza ctx.reply('type'=>'subscribed')
+ ctx.from.subscribe
+ ctx.from.add_to_roster
+ else
+ puts "**** #{ctx.from} is present"
+ end
+
+ ctx #returns a ctx for subsequent handlers if any
+ end
+
+ on_message do |ctx|
+ puts "**** message from #{ctx.from}"
+ puts ctx.body
+ hello = ctx.reply do |rep|
+ rep.body "hello world"
+ end
+ send_stanza hello
+
+ ctx #returns a ctx for subsequent handlers if any
+ end
+
+ on_exception(:anything) do |ctx|
+ p "rescued error"
+ raise ctx.env['error']
+ ctx
+ end
+
+ puts "***** friends list"
+ subscriptions = get_roster
+ subscriptions.each do |sub|
+ p sub.to_a
+ end
+ end
+end
+
+cfg = {:certificates => certdir}
+
+EM.run do
+ conn = EM::Xmpp::Connection.start(jid, pass, RosterClient, cfg)
+end
Please sign in to comment.
Something went wrong with that request. Please try again.