1
我使用xmpp4r並試圖獲得基本聊天功能,我希望稍後在我的Rails應用程序中實現的掛起。我在Ruby線程上的基礎知識仍然有點不穩定,所以我希望對此有所幫助。XMPP4R回調似乎不起作用
雖然我註冊回調,我沒有得到我的Gmail帳戶的迴應。我能夠發送消息,但我的ruby程序終止。爲了防止它終止,我試圖停止在程序中的線程,但我似乎無法讓它工作。
require 'rubygems'
require "xmpp4r/client"
require "xmpp4r/roster"
include Jabber
def connect
client = Client.new(JID::new("[email protected]"))
client.connect
client.auth("test")
client.send(Presence.new.set_type(:available))
client
end
def create_message(message, to_email)
msg = Jabber::Message::new(to_email, message)
msg.type = :chat
msg
end
def subscribe(email_id)
pres = Presence.new.set_type(:subscribe).set_to(email_id)
pres
end
client = connect
roster = Roster::Helper.new(client)
roster.add_subscription_request_callback do |item,pres|
roster.accept_subscription(pres.from)
end
def create_callback(client)
$t4= Thread.new do
client.add_message_callback do |m|
puts m.body
puts "................................Callback working"
end
end
end
puts "Client has connected"
msg = create_message("Welcome to the winter of my discontent", "[email protected]")
client.send(msg)
create_callback(client)
def check(client)
$t3 = Thread.new do
loop do
puts "t3 still running........."
Thread.current.stop
$t4.join
end
end
end
check(client)