2012-10-25 8 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 
/* 

が、それはメッセージを印刷します(「登録の成功を...」)とアプリケーションが終了し、それはdoesnの他の言葉では、コードに従わない、 "この後にいくつかのコード"を実行します。

私はこの問題を解決するために何ができるのですか?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です。

関連する問題