php - jaxl 返回调用它的函数

标签 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 库的人可以帮助我。

最佳答案

看起来您使用的代码与 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 帐户,它应该可以正常工作。

注意:请从存储库中 check out 最新代码。我进行了一些更新,这将允许重新初始化核心 JAXLLoop。如果您对细节感兴趣,这里是commit log .

关于php - jaxl 返回调用它的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13070783/

相关文章:

php - 查询数据并显示在html表格中

php - WordPress 的 super 菜单

xmpp - 如何使用 Smack 在 PubSub 节点上设置发布者?

php - 发送消息回调后的jaxl

php - Jaxl 类基础回调

php - __autoload() 没有被调用?

PHP密码保护足够安全

java - 如何使用 smack 库将自定义 IQ 节发送给另一个用户?

android - 使用 Jabber ID 的 Facebook 连接