dev-master
9999999-dev
MIT
The Requires
The Development Requires
by atipik
by Renaud LITTOLFF
This bundle allows you to use Hoa/Websocket/Server and Hoa/Websocket/Client into Symfony2., (*2)
With a simple configuration, you will be able to launch your WebSocket server, (*3)
Add these lines to your require section:, (*4)
{ "require": { "atipik/hoa-websocket-bundle" : "1.*@dev" } }
composer update
<?php # app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Atipik\Hoa\WebSocketBundle\AtipikHoaWebSocketBundle(), // ... ); return $bundles; } // ... }
To enable this bundle, you must add these lines in your config.yml
:, (*5)
# app/config/config.yml atipik_hoa_web_socket: ~
You can also specify address and port., (*6)
# app/config/config.yml atipik_hoa_web_socket: address : 1.3.3.7 port : 4242
By default, server will be started on 127.0.0.1:8080, (*7)
The Symfony 2 command php app/console hoa:websocketserver
has only one job: calling a runner., (*8)
The runner links modules (your logic code) and WebSocket events which are managed by Atipik\Hoa\WebSocketBundle\WebSocket\Server
., (*9)
When the WebSocket server receives an event, the runner catches it and calls all subscribed modules., (*10)
Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module
:<?php # src/My/Bundle/WebSocket/Module/MyModule.php namespace My\Bundle\WebSocket\Module; use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module; class MyModule extends Module { }
services.yml
:# src/My/Bundle/Resources/config/services.yml services: my_bundle.my_module: class: My\Bundle\WebSocket\Module\MyModule tags: - { name: atipik_hoa_web_socket.module }
Don't forget to add atipik_hoa_web_socket.module
tag !, (*11)
getSubscribedEvents
method:<?php # 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', ); } }
You can get the current bucket by using $this->getBucket()
., (*12)
<?php # 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'] ); } }
php app/console hoa:websocketserver
If you want to scale your server, you can affect modules to different group and launch server for one or more group., (*13)
You can affect many modules in the same group, (*14)
If you launch the server without group, all modules will be used., (*15)
getGroup()
method in your module:<?php # 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'; } // ... }
php app/console hoa:websocketserver -g foo
You can also specify more than one group:, (*16)
php app/console hoa:websocketserver -g foo -g bar
If you want to modify how the runner works, you should override its class:, (*17)
Atipik\Hoa\WebSocketBundle\WebSocket\Runner
.services.yml
:# src/My/Bundle/Resources/config/services.yml parameters: atipik_hoa_web_socket.runner.class: My\Bundle\WebSocket\Runner
If you want to modify how the WebSocket Server works, you should override its class:, (*18)
Atipik\Hoa\WebSocketBundle\WebSocket\Server
.services.yml
:# src/My/Bundle/Resources/config/services.yml parameters: atipik_hoa_web_socket.server.class: My\Bundle\WebSocket\Server
Hoa/Websocket allows you to override node class to add your own data., (*19)
Of course, this bundle allow you to specify which class to use:, (*20)
Hoa\Websocket\Node
:<?php # 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() { // ... } }
services.yml
:# src/My/Bundle/Resources/config/services.yml parameters: atipik_hoa_web_socket.node.class: My\Bundle\WebSocket\Node
<?php # 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 // .. } // ... }
If you want to communicate with a WebSocket Server, you can use service atipik_hoa_web_socket.client
by using $this->get('atipik_hoa_web_socket.client')
in your controller or to inject this service directly in services.yml., (*21)
For more documentation about WebSocket Client, see Hoa/WebSocket's documentation., (*22)
composer update ./vendor/bin/atoum
See Hoa/WebSocket's documentation) to know how to use How/WebSocket and to have an example of JavaScript code., (*23)
MIT