Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller\Component;
- use Cake\Controller\Component;
- use Cake\Core\Configure;
- use Cake\Log\Log;
- use ElephantIO\Client as Client;
- use ElephantIO\Engine\SocketIO\Version1X as Version;
- use ElephantIO\Exception\ServerConnectionFailureException;
- /**
- * Class SocketComponent
- * @package App\Controller\Component
- */
- class SocketComponent extends Component
- {
- /**
- * Sockets client
- *
- * @var Client
- */
- private $_sender;
- /**
- * Sockets connection url
- *
- * @var string
- */
- private $_url;
- /**
- * Building up a connection to socket
- *
- * @param array $config
- */
- public function initialize(array $config)
- {
- try {
- $this->_url = Configure::read('socket_url');
- $this->_sender = new Client(new Version($this->_url));
- $this->_sender->initialize();
- } catch (\Exception $e) {
- Log::warning('Cannot connect to socket server');
- }
- }
- /**
- * Sending data
- *
- * @param string $key
- * @param array $data
- */
- public function emit($key = 'broadcast', $data = [])
- {
- Log::info($data + ['user' => $key]);
- $this->_sender->emit($key, (array)$data);
- }
- /**
- * Closing connection
- */
- public function __destruct()
- {
- $this->_sender->close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment