# -*- 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
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.setType(IQ.Type.error); Element reason = DocumentHelper.createElement("reason"); reason.addNamespace("", NAME_SPACE); reason.setText("您没有足够多的礼物赠送了!"); reply.setChildElement(reason);
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
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插件开发续一