xmpp server: 負責發送數據到xmpp 服務器的中轉站

自己寫的一個簡單的xmpp服務的中轉站

require 'drb/drb'
require 'xmpp4r/client'
include Jabber

URI     = "druby://localhost:8787"
QUEUE   = Queue.new
ip_addr = "server地址"
server_str = "tester001@#{ip_addr}/testing" #用戶名@服務器地址/資源號(資源號可以任意設定)
jid     = JID::new(server_str)
password = '123456'
CLIENT   = Client::new(jid)
CLIENT.connect
CLIENT.auth(password)

class XmppServer
  

  def push(username, content)
    m = M.new(username, content)
    QUEUE.push m
  end

end

consumer = Thread.new {
  loop {
    m = QUEUE.pop
    puts m.username
    to   = "#{m.username}@weishanke.com/testing"
    body = m.content
    m    = Jabber::Message::new(to, body).set_type(:normal).set_id('1')
    CLIENT.send m
  }
}

class M

  attr_accessor :username, :content

  def initialize(username, content)
    @username = username
    @content  = content
  end
end



FRONT_OBJECT = XmppServer.new
DRb.start_service(URI, FRONT_OBJECT)
DRb.thread.join

一個簡單的客戶端程序

require 'drb/drb'

SERVER_URI="druby://localhost:8787"

DRb.start_service

xmppserver = DRbObject.new_with_uri(SERVER_URI)


10.times do |i| xmppserver.push("tester002", "testdddeeee....") sleep 1end

整合在Rails項目中比較簡單,將 如下代碼放到config/initializers/xmpp4r.rb  代碼

require 'drb/drb'

SERVER_URI="druby://localhost:8787"

DRb.start_service

Xmppserver = DRbObject.new_with_uri(SERVER_URI)

然後在程序中使用 Xmppserver.push username, content 就行
發佈了198 篇原創文章 · 獲贊 8 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章