2012-10-25 37 views
5

我已經使用庫XMPP交易jaxl庫:jaxl返回功能,這就是所謂的從

class xmpp{ 

     public function register_user($username, $password){ 
      require_once 'JAXL/jaxl.php'; 

      $this->client = new JAXL(array(
       'jid' => 'localhost', 
       'log_level' => JAXL_ERROR 
      ));   
      $this->username = $username; 
      $this->password = $password; 

      $this->client->require_xep(array(
       '0077' // InBand Registration 
      ));  
      $thisClassObject =& $this; 

      $this->client->add_cb('on_stream_features', function($stanza) use(&$thisClassObject) { 
       $thisClassObject->client->xeps['0077']->get_form('localhost'); 
       return array($thisClassObject, 'wait_for_register_form'); 
      }); 

      $this->client->start();  

      return; 
     } 

     public function wait_for_register_response($event, $args) { 


      if($event == 'end_stream') { 
       return; 
      } 
      else if($event == 'stanza_cb') { 
       $stanza = $args[0]; 
       if($stanza->name == 'iq') { 
       if($stanza->attrs['type'] == 'result') { 
        echo "registration successful".PHP_EOL."shutting down...".PHP_EOL; 
        $this->client->end_stream(); 
        return 'logged_out'; 
       } 
       else if($stanza->attrs['type'] == 'error') { 
        $error = $stanza->exists('error'); 
        echo "registration failed with error code: ".$error->attrs['code']." and type: ".$error->attrs['type'].PHP_EOL; 
        echo "error text: ".$error->exists('text')->text.PHP_EOL; 
        echo "shutting down...".PHP_EOL; 
        $this->client->end_stream(); 
        return "logged_out"; 
       } 
      } 
     } 
    } 

     public function wait_for_register_form($event, $args) { 

      $stanza = $args[0]; 
      $query = $stanza->exists('query', NS_INBAND_REGISTER); 
      if($query) { 
       $form = array(); 
       $instructions = $query->exists('instructions'); 
       if($instructions) { 
       echo $instructions->text.PHP_EOL; 
      } 

      $this->client->xeps['0077']->set_form($stanza->attrs['from'], array('username' => $this->username, 'password' => $this->password)); 
      return array($this, "wait_for_register_response"); 
     } 
     else { 
      $this->client->end_stream(); 
      return "logged_out"; 
     } 
     }  
    } 

這些代碼都是一樣register_user.php,但在一個類中實現的;

我以這種方式使用這個類在我的代碼:

$xmppObj = new xmpp(); 
$xmppObj('user','password'); 
/* 
some more code after this 
/* 

當它執行,成功創建用戶,但它的打印信息(「註冊成功的......」)和應用程序退出,它不」在類功能之後執行「此代碼之後」,換句話說,它不遵循代碼...

我該如何解決這個問題,一個人可以幫助我熟悉JAXL庫。

回答

1

看起來你幾乎在使用與examples/register_user.php內部相同的代碼。一旦用戶註冊成功後,腳本代碼的這部分封閉XMPPStream爲明顯:

if($stanza->attrs['type'] == 'result') { 
    echo "registration successful".PHP_EOL."shutting down...".PHP_EOL; 
    $this->client->end_stream(); 
    return 'logged_out'; 
} 

您必須改爲調用$client->send_end_stream();而不是$client->end_stream();。這將確保潛在的XMPPStream使適當的FSM state transition。同時爲on_disconnect事件添加回調,在此回調中,您可以再次嘗試使用新註冊的XMPP帳戶連接回來,並且它應該正常工作。

注意:請檢查存儲庫中的最新代碼。我做了一些更新,這將允許核心JAXLLoop被重新初始化。如果您對細節感興趣,請點擊這裏commit log