pzverr/websocket

Simple websocket server on php, based on morozovsk/websocket

dev-master 2017-11-29 15:02 UTC

README

Fork of morozovsk/websocket.
Add Custom options in Daemon Class.

Installation

{
    "require": {
        "pzverr/websocket": "dev-master",
    }
}

Symfony2 Example

AppBundle\Console\Command\WebSocketServerCommand.php

class WebSocketServerCommand extends ContainerAwareCommand
{
    ...
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine')->getManager();

        $servers = [
            'default' => [
                'class' => 'AppBundle\WebSocket\DefaultDaemonHandler',
                'pid' => '/tmp/websocket_default.pid',
                'websocket' => 'tcp://localhost:5001',
                'options' => [
                    'em' => $em,
                ]
            ]
        ];

        $action = $input->getArgument('action');

        $server = $input->getArgument('server');

        $WebSocketServer = new Server($servers[$server]);
        call_user_func(array($WebSocketServer, $action));
    }
}

AppBundle\Services\DefaultDaemonHandler.php

use pzverr\websocket\Daemon;
...
class DefaultDaemonHandler extends Daemon
{
    protected function onOpen($connectionId, $info)
    {
        $entity = $this->em->getRepository('FooBundle:Entity')->find(1);
        //etc
    }
}
...