ruby-on-rails - 获取在线用户 XMPP4r + Rails

标签 ruby-on-rails xmpp ejabberd xmpp4r

我正在尝试通过 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/

相关文章:

ruby-on-rails - 脚手架不更新 View

ruby-on-rails - 用于添加现场教程的 Rails gem

ssl - 通过 HTTPS 连接到 Prosody

xmpp - 为什么我不应该使用 couchdb 进行消息传递或实时事件流?

android - 如何使用 smack 4.1 的 XEP 198(流管理)?

ruby-on-rails - 渴望从 Rails 5 中实例化的 ActiveRecord 对象加载奇异关联的关联

Ruby 上的 PHP 风格 Web 开发 : creating microframework for that

ejabberd - Dockerfile:无法使用添加将本地文件映射到容器

ios - 如何启用用户注册 - eJabberd for mac?

erlang - 在 ets 和 mnesia 中哪个更有效