PHP code example of atipik / hoa-websocket-bundle

1. Go to this page and download the library: Download atipik/hoa-websocket-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

atipik / hoa-websocket-bundle example snippets



# app/AppKernel.php
// ...

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Atipik\Hoa\WebSocketBundle\AtipikHoaWebSocketBundle(),
            // ...
        );

        return $bundles;
    }

    // ...
}



# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
}


# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    public function getSubscribedEvents()
    {
        return array(
            'open'    => 'onOpen',
            'message' => 'onMessage',
        );
    }
}


# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    public function getSubscribedEvents()
    {
        return array(
            'open'    => 'onOpen',
            'message' => 'onMessage',
        );
    }

    public function onOpen()
    {
        $this->getLogger()->log('Here comes a new challenger !');
    }

    public function onMessage()
    {
        $data = $this->getBucket()->getData();

        $this->getLogger()->log(
            'Data received in %s: %s',
            __METHOD__,
            $data['message']
        );
    }
}


# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    // ...

    /**
     * Returns group name
     *
     * @return string
     */
    public function getGroup()
    {
        return 'foo';
    }

    // ...
}



# src/My/Bundle/WebSocket/Node.php
namespace My\Bundle\WebSocket;

class Node extends \Hoa\Websocket\Node
{
    protected $myData;

    public function getMyData()
    {
        return $this->myData;
    }

    public function setMyData($data)
    {
        $this->myData = $data;

        return $this;
    }

    public function doThingsWithMyData()
    {
        // ...
    }
}


# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    // ...

    public function onEvent1()
    {
        // ...

        $node = $this->getNode();

        $node->setMyData('foobar');

        // ..
    }

    // ...

    public function onEvent2()
    {
        // ...

        $node = $this->getNode();

        $node->getMyData(); // contain 'foobar' set in event1

        // ..
    }

    // ...
}