45 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			45 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?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");
							 |