xmpp4r实现了xmpp协议,使rails与openfire通信成为可能。废话不多少,看代码吧。
- 安装xmpp4r
gem install xmpp4r
- code
# -*- encoding : utf-8 -*- #该程序主要是向openfire发送信息 #用户已经在openfire注册了,测试帐号是:tester001,密码是123456 #接受信息帐号:tester002,密码: 123456 #openfire帐号已经启动了 require 'xmpp4r/client' include Jabber Jabber::debug = true # 开启jabber的debug模式 #----------------------------用户登录--------------------------------- server_str = 'tester001@192.168.1.126/testing' #用户名@服务器地址/资源号(资源号可以任意设定) jid = JID::new(server_str) password = '123456' cl = Client::new(jid) cl.connect cl.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模式启动才行
- 参考文档
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
- 补充
用户注册
server_str = "xxxxxxx@#{ip_addr}/testing" #用户名@服务器地址/资源号(资源号可以任意设定) jid = JID::new(server_str) password = '123456' cl = Client::new(jid) cl.connect fields = { "username" => "xxxxdxxx", "name" => "liuling", "password" => "123456" } cl.register(password, fields)