Skip to content

Commit

Permalink
Merge pull request #4 from Askedio/dev2
Browse files Browse the repository at this point in the history
ZMQ Push Server
  • Loading branch information
gcphost committed May 16, 2016
2 parents 6dff110 + a24c033 commit d6e2b83
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 27 deletions.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,31 @@ Register the `provider` in `config/app.php`.
~~~
Askedio\LaravelRatchet\Providers\LaravelRatchetServiceProvider::class,
~~~
Install [ZMQ](http://zeromq.org/intro:get-the-software) to use the `WampServer` driver.

# Example
[RatchetServerExample.php](https://github.com/Askedio/laravel-ratchet/blob/master/src/RatchetServerExample.php), a basic echo server, is used when you do not define a class. Here is another example:
~~~
apt-get install php7.0-zmq
~~~

# Push Server
The default driver will create a simple Push server based on [Ratchets example](http://socketo.me/docs/push).

~~~
php artisan ratchet:serve
Starting WampServer server on: 0.0.0.0:8080
Starting ZMQ server on: 127.0.0.1:5555
~~~

Create your own class based on [RatchetServerExample.php](https://github.com/Askedio/laravel-ratchet/blob/master/src/Pusher.php).

To insert data to ZMQ check out [the example](http://socketo.me/docs/push#editblogsubmission).


# Socket Server
Use the IoServer `driver` and the [RatchetServerExample.php](https://github.com/Askedio/laravel-ratchet/blob/master/src/RatchetServerExample.php) `class` to create a simple socket server.

Here is an example you could use in your `App`.
~~~
<?php
Expand All @@ -50,9 +72,9 @@ class RatchetServer extends RatchetServer
}
}
~~~
You'll need to change the class to `App\RatchetServer::class` in your command line or config.
You'll need to change the class to in your command line or config.
~~~
php artisan ratchet:serve --class="\App\RatchetServer::class"
php artisan ratchet:serve --driver=IoServer --class="App\RatchetServer:"
~~~

# Command Line
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
"keywords": ["laravel"],
"license": "MIT",
"type": "library",
"minimum-stability": "dev",

"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"cboden/ratchet": "^0.3.4",
"graham-campbell/throttle": "^5.2"
"graham-campbell/throttle": "^5.2",
"react/zmq": "0.2.*|0.3.*"
},
"autoload": {
"psr-4": {
Expand Down
154 changes: 151 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 42 additions & 12 deletions src/Console/Commands/RatchetServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Ratchet\WebSocket\WsServer;
use Symfony\Component\Console\Input\InputOption;



class RatchetServerCommand extends Command
{
/**
Expand Down Expand Up @@ -61,7 +63,7 @@ public function fire()

$this->port = intval($this->option('port'));

$this->info(sprintf('Starting server on: %s:%s', $this->host, $this->port));
$this->info(sprintf('Starting %s server on: %s:%d', $this->option('driver'), $this->host, $this->port));

$this->server($this->option('driver'))->run();
}
Expand All @@ -87,10 +89,6 @@ private function getDriver($driver)
return $this->getWsServerDriver($ratchetServer);
}

if ($driver == 'WampServer') {
return $this->getWampServerDriver($ratchetServer);
}

return $ratchetServer;
}

Expand All @@ -117,13 +115,40 @@ private function getWsServerDriver($ratchetServer)
*
* @return [type] [description]
*/
private function getWampServerDriver($ratchetServer)
private function startWampServer()
{
return $this->getWsServerDriver(
new WampServer(
$ratchetServer
)

$loop = \React\EventLoop\Factory::create();

$class = $this->option('class');

$ratchetServer = new $class($this);

$this->info(sprintf('Starting ZMQ server on: %s:%s', config('ratchet.zmq.host'), config('ratchet.zmq.port')));

$context = new \React\ZMQ\Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind(sprintf('tcp://%s:%d', config('ratchet.zmq.host'), config('ratchet.zmq.port')));

$pull->on('message', function($message) use ($ratchetServer) {
$ratchetServer->onEntry($message);
});


$webSock = new \React\Socket\Server($loop);
$webSock->listen($this->port, $this->host);
$webServer = new \Ratchet\Server\IoServer(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
new \Ratchet\Wamp\WampServer(
$ratchetServer
)
)
),
$webSock
);

return $loop;
}

/**
Expand All @@ -135,6 +160,11 @@ private function getWampServerDriver($ratchetServer)
*/
private function server($driver)
{

if ($driver == 'WampServer') {
return $this->startWampServer();
}

return IoServer::factory(
$this->getDriver($driver),
$this->port,
Expand All @@ -151,9 +181,9 @@ protected function getOptions()
{
return [
['host', null, InputOption::VALUE_OPTIONAL, 'Ratchet server host', config('ratchet.host', '0.0.0.0')],
['port', 'p', InputOption::VALUE_OPTIONAL, 'Ratchet server port', config('ratchet.port', 9090)],
['port', 'p', InputOption::VALUE_OPTIONAL, 'Ratchet server port', config('ratchet.port', 8080)],
['class', null, InputOption::VALUE_OPTIONAL, 'Class that implements MessageComponentInterface.', config('ratchet.class')],
['driver', null, InputOption::VALUE_OPTIONAL, 'Ratchet connection driver [IoServer|WsServer|WampServer]', 'WsServer'],
['driver', null, InputOption::VALUE_OPTIONAL, 'Ratchet connection driver [IoServer|WsServer|WampServer]', 'WampServer'],
];
}
}
Loading

0 comments on commit d6e2b83

Please sign in to comment.