• 给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


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    《Machine Learning in Action》—— 白话贝叶斯,“恰瓜群众”应该恰好瓜还是恰坏瓜
    《Machine Learning in Action》—— 女同学问Taoye,KNN应该怎么玩才能通关
    《Machine Learning in Action》—— Taoye给你讲讲决策树到底是支什么“鬼”
    深度学习炼丹术 —— Taoye不讲码德,又水文了,居然写感知器这么简单的内容
    《Machine Learning in Action》—— 浅谈线性回归的那些事
    《Machine Learning in Action》—— 懂的都懂,不懂的也能懂。非线性支持向量机
    《Machine Learning in Action》—— hao朋友,快来玩啊,决策树呦
    《Machine Learning in Action》—— 剖析支持向量机,优化SMO
    《Machine Learning in Action》—— 剖析支持向量机,单手狂撕线性SVM
    JVM 字节码指令
  • 原文地址:https://www.cnblogs.com/dingxiaoyue/p/4926851.html
Copyright © 2020-2023  润新知