Coder Social home page Coder Social logo

Comments (2)

Glaan avatar Glaan commented on July 26, 2024

In complement, here is my listener definition

use Fabiang\Xmpp\Event\XMLEvent;
use Fabiang\Xmpp\EventListener\AbstractEventListener;
use Fabiang\Xmpp\EventListener\BlockingEventListenerInterface;
use Fabiang\Xmpp\Protocol\User\User;

/**
 * Listener
 *
 * @package Xmpp\EventListener
 */
class Mam extends AbstractEventListener implements BlockingEventListenerInterface
{

    /**
     * Blocking.
     *
     * @var boolean
     */
    protected $blocking = false;

    /**
     * {@inheritDoc}
     */
    public function attachEvents()
    {
        $this->getOutputEventManager()
            ->attach('{urn:xmpp:mam:0}query', array($this, 'query'));
        $this->getInputEventManager()
            ->attach('{urn:xmpp:mam:0}fin', array($this, 'result'));
    }

    /**
     * Sending a query request for roster sets listener to blocking mode.
     *
     * @return void
     */
    public function query()
    {
        $this->blocking = true;
    }

    /**
     * Result received.
     *
     * @param \Fabiang\Xmpp\Event\XMLEvent $event
     * @return void
     */
    public function result(XMLEvent $event)
    {
        global $mamResult;
        $logger = $this->getOptions()->getLogger();
        
        if ($event->isEndTag()) {
            $xml = $event->getParameter(0)->ownerDocument;
            $xml->formatOutput = true;
            $logger->debug( $xml->saveXML() );
            $this->blocking = false;
        }
    }

    /**
     * {@inheritDoc}
     */
    public function isBlocking()
    {
        return $this->blocking;
    }
}

from xmpp.

Glaan avatar Glaan commented on July 26, 2024

I come back!
In fact, I misunderstood how the listener event works! I thinked that it returned the entirely buffer since the start of the function....
And so the problem came from my listener 😋
The parsing is true cause the answer is divided into severals answers of the type :

<message><result><message>....</message></result></message>

So now I create an event foreach tag "result" in buffer and the tag "fin" just close the listener.

Here is my new listener if you want to use it and integrate it ;)

<?php
/**
 * Listener
 *
 * @package Xmpp\EventListener
 */
class Mam extends AbstractEventListener implements BlockingEventListenerInterface
{

    /**
     * Blocking.
     *
     * @var boolean
     */
    protected $blocking = false;
    
    /**
     * Stock.
     *
     * @var boolean
     */
    protected $xmlResult;

    /**
     * {@inheritDoc}
     */
    public function attachEvents()
    {
        $this->getOutputEventManager()
            ->attach('{urn:xmpp:mam:0}query', array($this, 'query'));
        $input = $this->getInputEventManager();
        $input->attach('{urn:xmpp:mam:0}result', array($this, 'result'));
        $input->attach('{urn:xmpp:mam:0}fin', array($this, 'fin')); // dans le cas d'un set, renvoi une balise {urn:xmpp:mam:0}fin
    }

    /**
     * Sending a query request for roster sets listener to blocking mode.
     *
     * @return void
     */
    public function query()
    {
        $this->blocking = true;
        $this->xmlResult = new \DOMDocument();
    }

    /**
     * Result received.
     *
     * @param \Fabiang\Xmpp\Event\XMLEvent $event
     * @return void
     */
    public function result(XMLEvent $event)
    {
        if ($event->isEndTag()) {
            $xml = $event->getParameter(0);
            $node = $this->xmlResult->importNode($xml, true);
            $this->xmlResult->appendChild($node);
        }
    }

    /**
     * Result received.
     *
     * @param \Fabiang\Xmpp\Event\XMLEvent $event
     * @return void
     */
    public function fin(XMLEvent $event)
    {
        global $mamResult;
        $logger = $this->getOptions()->getLogger();
        
        if ($event->isEndTag()) {
            $xml = $event->getParameter(0);
            $this->logDomElement($xml);
            $this->xmlResult->formatOutput = true;
            $mamResult = $this->xmlResult->saveXML();
            $this->blocking = false;
        }
    }

    /**
     * {@inheritDoc}
     */
    public function isBlocking()
    {
        return $this->blocking;
    }
    
    public function logDomElement(\DOMElement $xml)
    {
        $newdoc = new \DOMDocument();
        $newdoc->appendChild($newdoc->importNode($xml,true));
        $newdoc->formatOutput = true;
        $logger = $this->getOptions()->getLogger();
        $logger->debug("Contenu du DOMElement : \n".$newdoc->saveXML());
    }
}

Thanks again for this library 👍

from xmpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.