请问各位大虾:
    有谁用php做个IM的?
    xmpphp这个类库有接触的没?
    现在小弟遇到的问题是,不能持久性的连接?不能够获取到花名册(用户好友列表)?
     
    例:
    <?php
        try {
           $conn->connect();
           $conn->processUntil('session_start');
           $conn->roster->getRoster();
  $conn->disconnect();
       } catch(XMPPHP_Exception $e) {
           die($e->getMessage());
       }
     这样的代码不能够获取到花名册?
     有了解这个的吗?希望指点指点!

解决方案 »

  1.   

    发出去:
    include 'XMPPHP/XMPP.php'; 
    $conn = new XMPPHP_XMPP(XMPP_SERVER_IP, XMPP_SERVER_PORT, XMPP_SERVER_USER, 
    XMPP_SERVER_PASSWORD, XMPP_SERVER_DOMAIN);
    $to_jid = 'id';
    $body = 'content';
    $type = 'chat';
    try {
        $conn->connect();
        $conn->processUntil('session_start');
        $conn->message($to_jid,$body,$type);
        $conn->disconnect();
        echo $this->_success_log;
    } catch(XMPPHP_Exception $e) {
        die($this->_error_log);
    }
    接受要是个while,死循环,一直开启的后台进程,一直运行的php文件
    例如:
    long_run.php<?php 
    // activate full error reporting
    ignore_user_abort(true);
    set_time_limit(0); 
     
    include_once dirname(__FILE__) . '/xmppdefine.php';
    include_once (dirname(__FILE__) . '/XMPPHP/XMPP.php');   $conn = new XMPPHP_XMPP(XMPP_SERVER_IP, XMPP_SERVER_PORT, XMPP_SERVER_USER, 
    XMPP_SERVER_PASSWORD, XMPP_SERVER_DOMAIN);
    $conn->autoSubscribe();
    $vcard_request = array();try {
        $conn->connect();     
        while (true) { //!$conn->isDisconnected()
            $payloads = $conn->processUntil(
            array(
                'message', 
                //'presence', 
                //'end_stream', 
                'session_start', 
                //'vcard'
            ));
            foreach ($payloads as $event) {
                
                $pl = $event[1];
                switch ($event[0]) {
                case 'message':
                    //////////////////////消息的接收处理!!!!!!
                    break;
                   
                case 'presence':
                    ;
                    break;
                case 'session_start':
                   
                    $conn->getRoster();
                    $conn->presence($status = "Cheese!");
                    break;
                case 'vcard':
                    // check to see who requested this vcard
                    $deliver = array_keys($vcard_request, 
                    $pl['from']);
                    // work through the array to generate a message
                    print_r($pl);
                    $msg = '';
                    foreach ($pl as $key => $item) {
                        $msg .= "$key: ";
                        if (is_array($item)) {
                            $msg .= "\n";
                            foreach ($item as $subkey => $subitem) {
                                $msg .= "  $subkey: $subitem\n";
                            }
                        } else {
                            $msg .= "$item\n";
                        }
                    }
                    // deliver the vcard msg to everyone that requested that vcard
                    foreach ($deliver as $sendjid) {
                        // remove the note on requests as we send out the message
                        unset($vcard_request[$sendjid]);
                        //$conn->message($sendjid, $msg, 'chat');
                    }
                    break; 
                }
               
            }
            
            
        
        }
    }
    catch (XMPPHP_Exception $e) {
        die("error:".$e->getMessage());
    }
    $conn->disconnect();
      

  2.   


    哦!你能够获取到花名册吗?
    还有服务端需要他保存持久在线不?
    需要其他的类库来配合操作吗?
    你有一个完成的demo?或者实例不?