openfire插件开发续一

Openfire插件开发环境已经搭建起来了,现在开始开发了。
技术:xmpp4r, java, openfire, xmpp,jabber

需求定义:
1,用户注册的时候,需要定义一些新的字段: age, gender, mood等
2,用户可以修改个人的信息和心情(mood)
3,用户可以给指定的用户发送礼物

开发过程:
xmpp的关键节点

presence: 控制并报告实体的可访问性
message: 一个实体向另外的实体发送消息,还可以使用来传输任何类型的结构化数据
         发送后不管类型,没有内在的可靠性
iq: 信息与查询,提供请求与响应机制
     type: get, set, result, error

开发插件其实就是扩展这些节点,我扩展的是 IQ 节点的命名空间namespace, xmlns

xmpp4r的测试代码如下
# -*- encoding : utf-8 -*-
#该程序主要是向openfire发送信息
#用户已经在openfire注册了,测试帐号是:tester001,密码是123456
#接受信息帐号:tester002,密码: 123456
#openfire帐号已经启动了
require 'xmpp4r'
require 'xmpp4r/client'
require 'xmpp4r/roster/iq/roster'
include Jabber

Jabber::debug = false # 开启jabber的debug模式

#----------------------------用户登录---------------------------------
server_str = '[email protected]/testing' #用户名@服务器地址/资源号(资源号可以任意设定)
jid = JID::new(server_str)
password = '123456'
cl = Client::new(jid)
cl.connect
cl.auth(password)

#----------------------------发送简单的消息-------------------------------
to = "[email protected]/testing"
subject = "测试xmpp4r,发送消息"
body = "此处是消息的主题部分,应该可以看到很多字哦。"
m = Message::new(to, body).set_type(:normal).set_id('1').set_subject(subject)
cl.send m

#----------------------------取得注册的基本信息register----------------------
#openfire的返回字段:[nil, ["username", "password", "email", "name", "registered"]]
puts "可以看到注册信息哦----------------"
rinfo = cl.register_info
puts rinfo.inspect

#----------------------------注册会员-----------------------------------------
puts "开始注册了,兄弟:)"
#iq_reg = Iq.new_register("user_from_xmpp4r","123456");
#puts iq_reg.inspect

iq_reg = Iq.new(:set)
query = IqQuery.new
query.add_namespace('jabber:iq:register')
query.add(REXML::Element.new('username').add_text("hexudong"))
query.add(REXML::Element.new('password').add_text("123456"))
query.add(REXML::Element.new('age').add_text("25"))
query.add(REXML::Element.new('gender').add_text("m"))
query.add(REXML::Element.new('mood').add_text("今天天气不错~~~"))
iq_reg.add(query)

cl.send(iq_reg)

#---------------------------修改个人信息哦--------------------------------------------
puts "修改个人信息了哦"
iq_update = Iq.new(:set)
query = IqQuery.new
query.add_namespace('jabber:iq:updateuserinfo')
query.add(REXML::Element.new('username').add_text("hexudong"))
query.add(REXML::Element.new('age').add_text("26"))
query.add(REXML::Element.new('gender').add_text("m"))
query.add(REXML::Element.new('mood').add_text("来吧,兄弟们~~~"))
iq_update.add(query)

cl.send(iq_update)

#发送鲜花,xml:
#<iq id='gift' type='get'>
#<query xmlns='openfire:rayclear:gift'>
#<host>[email protected]/testing</host>
#</query>
#</iq>
puts "开始发送鲜花了,:)"
iq = Iq.new(:get)
iq.add_attribute("id", "gift")
namespace = "openfire:rayclear:gift"
query = IqQuery.new
query.add_namespace(namespace)
query.add(REXML::Element.new('host').add_text("[email protected]/testing"))
iq.add(query)
puts cl.send  iq


# Shutdown
cl.close


xmpp4r 如何构造 xmpp节点?
      iq = Iq.new(:set)
      bind = iq.add REXML::Element.new('bind')
      bind.add_namespace @stream_features['bind']
      if desired_resource
        resource = bind.add REXML::Element.new('resource')
        resource.text = desired_resource
      end


Element userIdentity = DocumentHelper.createElement("identity");
        userIdentity.addAttribute("category", "account");
        userIdentity.addAttribute("type", "anonymous");
        anonymousUserIdentities.add(userIdentity);
        userIdentity = DocumentHelper.createElement("identity");
        userIdentity.addAttribute("category", "account");
        userIdentity.addAttribute("type", "registered");
        registeredUserIdentities.add(userIdentity);
        userFeatures.add(NAMESPACE_DISCO_INFO);

如上的代码可以参考用于构造 节点

补充代码,如何给reply的xml加子字节?
reply.setType(IQ.Type.error);
			Element reason = DocumentHelper.createElement("reason");
			reason.addNamespace("", NAME_SPACE);
			reason.setText("您没有足够多的礼物赠送了!");
			reply.setChildElement(reason);


xmpp4r 如何取得xmpp server的返回值?
cl.send iq
cl.add_iq_callback do |tiq|
  puts "come here: -------------:" + tiq.to_s
 # debugger
  puts tiq.to
  puts tiq.elements["reason"].text
  #cl.send iq
end
sleep 30
返回的 tiq 是一个 REXML::Element
所以,可以通过 tiq.elements["reason"].text 取值

如下是一个完整的参考例子
require 'xmpp4r/client'

# Create a *very* simple dictionary using a hash
hash = {}
hash['ruby'] = 'Greatest little object oriented scripting language'
hash['xmpp4r'] = 'Simple XMPP library for ruby'
hash['xmpp'] = 'Extensible Messaging and Presence Protocol'

# Connect to the server and authenticate
jid = Jabber::JID::new('[email protected]/Home')
cl = Jabber::Client::new(jid)
cl.connect
cl.auth('password')

# Indicate our presence to the server
cl.send Jabber::Presence::new

# Send a salutation to a given user that we're ready
salutation = Jabber::Message::new( '[email protected]', 'DictBot ready' )
salutation.set_type(:chat).set_id('1')
cl.send salutation 

# Add a message callback to respond to peer requests
cl.add_message_callback do |inmsg|

    # Lookup the word in the dictionary
    resp = hash[inmsg.body]
    if resp == nil
      resp = "don't know about " + inmsg.body
    end

    # Send the response
    outmsg = Jabber::Message::new( inmsg.from, resp )
    outmsg.set_type(:chat).set_id('1')
    cl.send outmsg

end

# Run
while 1
end



openfire如何在服务器端截取字符串


相关文章:

openfire插件开发  

openfire插件开发续一

你可能感兴趣的:(openfire插件开发续一)