rails for openfire: xmpp4r使用实践

来源:互联网 发布:怎么看金十数据k 编辑:程序博客网 时间:2024/05/21 08:12
xmpp4r实现了xmpp协议,使rails与openfire通信成为可能。废话不多少,看代码吧。

  1. 安装xmpp4r
    gem install xmpp4r

  2. code
    # -*- encoding : utf-8 -*-#该程序主要是向openfire发送信息#用户已经在openfire注册了,测试帐号是:tester001,密码是123456#接受信息帐号:tester002,密码: 123456#openfire帐号已经启动了require 'xmpp4r/client'include JabberJabber::debug = true # 开启jabber的debug模式#----------------------------用户登录---------------------------------server_str = 'tester001@192.168.1.126/testing' #用户名@服务器地址/资源号(资源号可以任意设定)jid = JID::new(server_str)password = '123456'cl = Client::new(jid)cl.connectcl.auth(password)#----------------------------发送简单的消息-------------------------------to = "tester002@192.168.1.126/testing"subject = "测试xmpp4r,发送消息"body = "此处是消息的主题部分,应该可以看到很多字哦。"m = Message::new(to, body).set_type(:normal).set_id('1').set_subject(subject)cl.send m

    openfire的安装,启动和配置请参考:http://blog.csdn.net/hexudong08/article/details/7369435

    ***如果spark想受到信息,必须以debug模式启动才行

  3. 参考文档
    http://devblog.famundo.com/articles/2006/10/14/ruby-and-xmpp-jabber-part-2-logging-in-and-sending-simple-messages
    http://devblog.famundo.com/articles/2006/10/18/ruby-and-xmpp-jabber-part-3-adding-html-to-the-messages
    github source: https://github.com/ln/xmpp4r

  4. 补充
    用户注册
    server_str = "xxxxxxx@#{ip_addr}/testing" #用户名@服务器地址/资源号(资源号可以任意设定)jid = JID::new(server_str)password = '123456'cl = Client::new(jid)cl.connectfields = {  "username" => "xxxxdxxx",  "name"     => "liuling",  "password" => "123456"}cl.register(password, fields)






原创粉丝点击