jaxl返回它被调用的函数

时间:2012-10-25 14:25:37

标签: php xmpp xmpphp

我有使用jaxl库的xmpp事务库:

    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 个答案:

答案 0 :(得分: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