Initial commit
This commit is contained in:
33
examples/local.php
Normal file
33
examples/local.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/../vendor/autoload.php";
|
||||
|
||||
use NoccyLabs\React\CommandBus\CommandBus;
|
||||
use NoccyLabs\React\CommandBus\CommandRegistry;
|
||||
use NoccyLabs\React\CommandBus\Context;
|
||||
use React\EventLoop\Loop;
|
||||
use React\Promise\Promise;
|
||||
|
||||
$commands = new CommandRegistry();
|
||||
$commands->register("hello", function (Context $context) {
|
||||
return new Promise(function (callable $resolve) use ($context) {
|
||||
Loop::addTimer(1, function () use ($context, $resolve) {
|
||||
$resolve("Hello, {$context->name}");
|
||||
});
|
||||
});
|
||||
});
|
||||
$commands->register("hello2", function (Context $context) {
|
||||
return "Hello2, {$context->name}";
|
||||
});
|
||||
|
||||
$bus = new CommandBus($commands);
|
||||
|
||||
$bus->execute('hello', ['name'=>'Bob'])
|
||||
->then(function ($result) {
|
||||
var_dump($result);
|
||||
});
|
||||
|
||||
$bus->execute('hello2', ['name'=>'Bob'])
|
||||
->then(function ($result) {
|
||||
var_dump($result);
|
||||
});
|
45
examples/server.php
Normal file
45
examples/server.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/../vendor/autoload.php";
|
||||
|
||||
use NoccyLabs\React\CommandBus\CommandBus;
|
||||
use NoccyLabs\React\CommandBus\CommandBusClient;
|
||||
use NoccyLabs\React\CommandBus\CommandRegistry;
|
||||
use NoccyLabs\React\CommandBus\Context;
|
||||
use React\Promise\Promise;
|
||||
use React\Socket\SocketServer;
|
||||
|
||||
$commands = new CommandRegistry();
|
||||
// Register some function to call. The name here is "hello", and it will
|
||||
// receive a Context holding the call context. Any passed data will be
|
||||
// available as properties on the Context object.
|
||||
$commands->register("hello", function (Context $context) {
|
||||
// You don't have to, but you should return a promise from your
|
||||
// commands.
|
||||
return new Promise(function (callable $resolve) use ($context) {
|
||||
$resolve("Hello, {$context->name}");
|
||||
});
|
||||
});
|
||||
|
||||
// Create the CommandBus and pass the CommandRegistry
|
||||
$bus = new CommandBus($commands);
|
||||
|
||||
$server = new SocketServer("tcp://127.0.0.1:9999");
|
||||
$bus->addServer($server);
|
||||
|
||||
// The server is sorted, now for the client!
|
||||
|
||||
$client = new CommandBusClient();
|
||||
$client->on(CommandBusClient::EVENT_CONNECTED,
|
||||
function () use ($client, $bus) {
|
||||
$client->execute('hello', ['name'=>'Bob'])
|
||||
->then(function ($result) use ($client,$bus) {
|
||||
// Result from the call
|
||||
var_dump($result);
|
||||
// Shut down after receiving the response
|
||||
$bus->close();
|
||||
$client->close();
|
||||
});
|
||||
}
|
||||
);
|
||||
$client->connect("tcp://127.0.0.1:9999");
|
Reference in New Issue
Block a user