我正在使用xmpp4r并试图获得我希望稍后在我的Rails应用程序中实现的基本聊天功能。我在Ruby Threads上的基础知识仍然有点摇摇欲坠,所以我很感激任何帮助。
虽然我注册了回调但我没有得到我的Gmail帐户的回复。我能够发送消息,但我的ruby程序终止。为了防止它终止我试图停止程序中的线程,但我似乎无法让它工作。
require 'rubygems'
require "xmpp4r/client"
require "xmpp4r/roster"
include Jabber
def connect
client = Client.new(JID::new("test@gmail.com"))
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", "test@tech.com")
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)
答案 0 :(得分:-1)
除了check(client)
之外,您可以像使用XMPP4R示例一样使用Thread.stop
。
如果您想查看XMPP级别的内容,请使用Jabber::debug = true
启用控制台调试输出。