Created
January 3, 2010 02:22
-
-
Save santiago/267780 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'eventmachine' | |
#require 'xmpp4r' | |
require 'xmpp4r-simple' | |
__DIR__ = File.dirname File.expand_path(__FILE__) | |
EM.run { | |
class My_Client | |
def run | |
jid= ('[email protected]') | |
password= 'XXX' | |
socket= Jabber::Simple.new(jid,password) | |
EM::PeriodicTimer.new(0.05) do | |
socket.received_messages do |msg| | |
puts "Received message:#{msg}" | |
end | |
end | |
socket | |
end | |
end | |
class Chat_Server < EM::Connection | |
def self.start host, port | |
puts ">> Chat Server started on #{host}:#{port}" | |
EM.start_server host, port, self | |
end | |
def post_init | |
unless @socket | |
client= My_Client.new | |
@socket= client.run | |
end | |
@buf = BufferedTokenizer.new("\0") | |
@ip = Socket.unpack_sockaddr_in(get_peername).last rescue '0.0.0.0' | |
puts ">> Chat Server got connection from #{@ip}" | |
end | |
def unbind | |
@timer.cancel if @timer | |
puts ">> Chat Server got disconnect from #{@ip}" | |
end | |
def receive_data data | |
if data.strip == "<policy-file-request/>" | |
send %[ | |
<?xml version="1.0"?> | |
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> | |
<cross-domain-policy> | |
<allow-access-from domain="*" to-ports="*" /> | |
</cross-domain-policy> | |
] | |
close_connection_after_writing | |
return | |
end | |
# send message to XMPP | |
to= "[email protected]" | |
subject= "santiago says:" | |
body= "#{data}" | |
# m= Message::new(to,body).set_type(:normal).set_id('1').set_subject(subject) | |
# @cl.send m | |
deliver(to,body) | |
send body | |
# get messages from xmpp | |
# timer here | |
end | |
def send data | |
send_data "#{data}" | |
end | |
def deliver(to,data) | |
jid= '[email protected]' | |
password= 'S4ntiag0' | |
# @socket= Jabber::Simple.new(@jid,@password) | |
@socket.deliver(to,data) | |
end | |
end | |
Chat_Server.start 'localhost', 1234 | |
} | |
this is what i get in the XMPP log whenever "deliver" from xmpp4r-simple is called (same for send method from xmpp4r) | |
=INFO REPORT==== 2010-01-02 21:19:57 === | |
I(<0.3152.0>:ejabberd_c2s:1329) : ({socket_state,tls,{tlssock,#Port<0.2044>,#Port<0.2046>},<0.3151.0>}) Close session for [email protected]/33593208081262485196971315 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment