我正在尝试通过 XMPP 服务器(Ejabberd)中的用户获取在线好友。我正在使用 Ruby on Rails 3.2。 这个想法是在数组中添加所有在线用户以在 View 页面上使用它。
我找到了异步代码(如下),但它使用线程并且很难在 Controller 方法上工作。
jid = Jabber::JID.new('user@localhost')
cl = Jabber::Client.new(jid)
cl.connect
cl.auth('123456')
@online_users = [] #online users queue
roster = Jabber::Roster::Helper.new(cl)
mainthread = Thread.current
roster.add_presence_callback { |item,oldpres,pres|
if item.online?
@online_users.push item
else
@online_users.delete_if {|x| x.jid == item.jid }
end
puts @online_users.inspect
puts "#{item.jid} - online: #{item.online?}"
}
cl.send(Jabber::Presence.new.set_show(:dnd))
t = Thread.new { sleep XMPP_REQUEST_TIMEOUT; mainthread.wakeup;}
Thread.stop
cl.close
所以我需要一些同步代码,或者某种方式在 Controller 方法中执行此类代码。
谢谢。
最佳答案
为此找到了另一个对我有帮助的解决方案:
我安装了mod_rest在 ejabberd 服务器中。该模块允许您对ejabberdctl的终端命令进行HTTP请求。
所以它有“ejabberdctlconnected_users”,返回在线用户。
因此,在使用 gem rest-client 的模型应用程序中,您可以执行类似的操作:
def online_users
response = RestClient.post('http://localhost:5280/rest', "connected_users")
response
end
关于ruby-on-rails - 获取在线用户 XMPP4r + Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10707084/