给xmpphp添加了几个常用的方法
给xmpphp添加给了以下的常用方法:
registerNewUser //注册一个新用户
addRosterContact //发送添加好友的请求
accept friend request //接受好友请求
deleteRosterContact //删除某个好友
roomMessage //发送群聊消息
createChatRoom //创建群聊
kickUserOutToChatRoom //把某个人剔除群聊
/**
* Register a new user.
*
* @param $entity
* Entity we want information about
*/
public function registerNewUser($user_name, $password = NULL, $email, $name=NULL){ $id = 'reg_' . $this->getID();
$xml = "<iq type='set' id='$id'>
<query xmlns='jabber:iq:register'>
<username>" . $user_name . "</username>
<password>" . $password . "</password>
<email>" . $email . "</email>
<name>" . $name . "</name>
</query>
</iq>";
$this->send($xml); } /**
* Add contact to your roster
*/
public function addRosterContact($jid, $name, $nickName="",$groups=array("Friends")){
// return if there is no jid specified
if(!$jid) return;
// set name to the jid if none is specified
if (!$name) { $name = $jid; }
$id = $this->getID();
$xml = "<iq type='set' id='$id'>";
$xml .= "<query xmlns='jabber:iq:roster'>";
$xml .= "<item jid='$jid' name='$name'>";
foreach ($groups as $group) {
$xml .= "<group>$group</group>";
}
$xml .= "</item>";
$xml .= "</query>";
$xml .= "</iq>"; $xml= <<<EOF
<presence to='{$jid}' type='subscribe'>
<nick xmlns='http://jabber.org/protocol/nick'>{$nickName}</nick>
</presence>
EOF; $this->send($xml);
} /**
* accept friend request
* @param unknown_type $send_jid
* @param unknown_type $received_jid
*/
public function acceptRosterRequest( $send_jid, $receive_jid,$send_name="",$receive_name="" ){ $xml= <<<EOF
<presence from="{$send_jid}" to="{$receive_jid}" type="subscribed">
<nick xmlns='http://jabber.org/protocol/nick'>{$send_name}</nick>
</presence>
<presence from="{$receive_jid}" to="{$send_jid}" type="subscribed">
<nick xmlns='http://jabber.org/protocol/nick'>{$receive_name}</nick>
</presence>
EOF;
$this->send($xml); } /**
* Contact you wish to remove
* @param $jid
*
*/
public function deleteRosterContact($jid) {
$id = $this->getID();
$xml = "<iq type='set' id='$id'>";
$xml .= "<query xmlns='jabber:iq:roster'>";
$xml .= "<item jid='" . $jid . "' subscription='remove' />";
$xml .= "</query>";
$xml .= "</iq>";
$this->send($xml);
} /**
* send group message
* @param unknown_type $to
* @param unknown_type $body
* @param unknown_type $type
* @param unknown_type $subject
* @param unknown_type $payload
*/
public function roomMessage($jid,$room_jid, $body, $subject = null, $payload = null, $user_name=null) { if( $user_name ){
// $present_roomId=$room_jid."/".$user_name;
$present_roomId=$room_jid."/".$user_name."_"; //加“_”是因为防止同一个帐号在两个地方用同样的nickname登录房间,会有一个地方会退出登录的
}
else{
$present_roomId=$room_jid;
} $id=$this->getID(); $out= <<<EOF
<presence
from='{$jid}'
to='{$present_roomId}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence> EOF; $jid = htmlspecialchars($jid);
$body = htmlspecialchars($body);
$subject = htmlspecialchars($subject); $out .= "<message from=\"{$jid}\" to=\"{$room_jid}\" type='groupchat'>";
if($subject) $out .= "<subject>$subject</subject>";
$out .= "<body>$body</body>";
if($payload) $out .= $payload;
$out .= "</message>"; $this->send($out); } /**
* create chat group
* @param unknown_type $jid, creater's jid
* @param unknown_type $room_jid chatroom's jid
*
* take example
* in a class, how to use this methodd
*
//modify room default setting
$room_setting=array('muc#roomconfig_roomname'=>$testName,'muc#roomconfig_roomdesc'=>$testDesc,'muc#roomconfig_changesubject'=>1); //create chatroom , save chatroom settting
$this->_conn->createChatRoom($jid,$test_id,$real_name,$room_setting,$this); //set chatroom setting to xmpp server, in this class, need a public $room_xml
$this->_conn->sendChatroom_setting($jid,$test_id,$real_name,$this->room_xml);
*
*/
public function createChatRoom($jid, $room_jid,$real_name,$room_setting=array(),$ref_obj){ $this->refObj=$ref_obj;
$this->room_setting=$room_setting;
$id=$this->getID(); $this->from=$jid;
$this->to=$room_jid; $xml= <<<EOF
<presence
from='{$jid}'
to='{$room_jid}/{$real_name}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
<iq from='{$jid}'
id="{$id}"
to='{$room_jid}'
type='get'>
<query xmlns='http://jabber.org/protocol/muc#owner'/>
</iq>
EOF; $this->addIdHandler($id, 'setChatroom');
$this->send($xml);
} /**
* set chatroom setting
* @param unknown_type $xml
*/
public function setChatroom($xml){ $xml->attrs['type']="set";
$xml->attrs['from']=$this->from;
$xml->attrs['to']=$this->to;
$xml->subs[0]->subs[0]->attrs['type']='submit';
foreach( $xml->subs[0]->subs[0]->subs as &$node ){
if( isset($node->attrs['var']) && isset( $this->room_setting[$node->attrs['var']] ) ){
$node->subs[0]->data=$this->room_setting[$node->attrs['var']];
}
} $this->refObj->room_xml=$xml->toString();
} /**
* please call setChatroom() before call this method
*/
public function sendChatroomSetting($jid, $room_jid,$real_name,$room_xml){ $xml= <<<EOF
<presence
from='{$jid}'
to='{$room_jid}/{$real_name}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
EOF; $xml.=$room_xml;
$this->send($xml); } /**
* kick user out to chat room
* @param unknown_type $jid, create's jid
* @param unknown_type $room_jid,chatroom's jid
* @param unknown_type $real_name,kickout user's jid
* @param unknown_type $room_jid,chatroom's jid
*/
public function kickUserOutToChatRoom($jid, $room_jid,$real_name,$kick_names=array()){ $xml= <<<EOF
<presence
from='{$jid}'
to='{$room_jid}/{$real_name}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
EOF; foreach( $kick_names as $kick_name ){ $id=$this->getID(); $xml.= <<<EOF
<iq from='{$jid}'
id='{$id}'
to='{$room_jid}'
type='set'>
<query xmlns='http://jabber.org/protocol/muc#admin'>
<item nick='{$kick_name}' role='none'>
<reason>manger kick you out of room</reason>
</item>
</query>
</iq>
EOF; } $this->send($xml);
} /**
*
* @param XML Object $xml
*/
protected function delete_roster_contact_handler($xml) {
// do any handling you wish here
$this->event('contact_removed');
} public function getJid(){
return $this->jid;
}
代码已开源,地址:
https://github.com/newjueqi/xmpp
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2014.06.25更新
关于创建群组的例子,请看XMPPHP/test.php 文件中的 createEventChatRoom()
[文章作者]曾健生
[作者邮箱]h6k65@126.com
[作者QQ]190678908
[新浪微博] @newjueqi
[博客] http://blog.csdn.net/newjueqi
http://blog.sina.com.cn/h6k65
版权声明:本文为博主原创文章,未经博主允许不得转载。
给xmpphp添加了几个常用的方法的更多相关文章
- WebAPi添加常用扩展方法及思维发散
前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...
- iOS常用公共方法
iOS常用公共方法 字数2917 阅读3070 评论45 喜欢236 1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat si ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- Delphi中TStringList类常用属性方法详解
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...
- (转)Android之常用功能方法大集合
这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...
- php中的常用魔术方法总结
以下是对php中的常用魔术方法进行了详细的总结介绍,需要的朋友可以过来参考下 常用的魔术方法有:__Tostring () __Call() __autoLoad() __ clone() __GET ...
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- js中关于string的一些常用的方法
最近总结了一些关于string中的常用方法, 其中大部分的方法来自于<JavaScript框架设计>这本书, 如果有更好的方法,或者有关于string的别的常用的方法,希望大家不吝赐教. ...
- python字符串常用的方法解析
这是本人在学习python过程中总结的一些关于字符串的常用的方法. 文中引用了python3.5版本内置的帮助文档,大致进行翻译,并添加了几个小实验. isalnum S.isalnum() -> ...
随机推荐
- 【Qt编程】基于Qt的词典开发系列<十二>调用讲述人
我们知道,win7系统自带有讲述人,即可以机器读出当前内容,具体可以将电脑锁定,然后点击左下角的按钮即可.之前在用Matlab写扫雷游戏的时候,也曾经调用过讲述人来进行游戏的语音提示.具体的Matla ...
- ActiveMQ系列之五:ActiveMQ的Transport
连接到ActiveMQ Connector:ActiveMQ提供的,用来实现连接通讯的功能.包括:client-to-broker.broker-to-broker. ActiveMQ允许客户端使用多 ...
- java获取时间
string startTimeStr = ((String) jsonCampaign.get(configObj.getKeyword(config.START_TIME))); ...
- Ubuntu14.04安装androidStudio错误解除
错误1 ubuntu androidStudio :app:mergeDebugResources FAILED 办法: sudo dpkg --add-architecture i386 sudo ...
- 知物由学|游戏开发者如何从容应对Unity手游风险?
本文由 网易云发布. "知物由学"是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不 ...
- 排序算法入门之归并排序(java实现)
归并排序是采用分治法的典型应用. 参考<数据结构与算法分析-Java语言描述> 归并排序其实要做两件事: (1)"分解"--将序列每次折半划分. (2)"合并 ...
- DB2常用函数
1.char函数 char(current date,ISO)--转换成yyyy-mm-dd char(current date,USA)--转换成mm/dd/yyyy char(current ...
- storm中的Scheduler
Scheduler是storm的调度器,负责为topology分配当前集群中可用的资源.Storm分别提供了3中调度器: EvenScheduler:会将系统中的可用资源均匀地分配给当前需要任务分配的 ...
- Spring消息之STOMP
一.STOMP 简介 直接使用WebSocket(或SockJS)就很类似于使用TCP套接字来编写Web应用.因为没有高层级的线路协议(wire protocol),因此就需要我们定义应用之间所发送消 ...
- Js 浅克隆详解
浅克隆:不仅赋值,而且赋予了内存地址深度克隆:赋值,内存地址不同var a = [1,2,3]; var b = a; a = [4,5,6]; alert(b); //[1,2,3] 面试时被问到这 ...