public function renderDefault() { // Get server $server = new Lightbulb\Json\Rpc2\Server(); $server->access = new AccessHandler(); $server->user = new UserHandler(); // Supress handling of output, as we are sending it differently $server->supressOutput(); // When running the request, we need to check whether there has been anything returned. If not, // then we have to send an empty text response, as Nette\...\JsonResponse throws exception // on empty responses. $response = $server->handle(); if (!empty($response) || is_array($response)) { // is_array in order to send [] $this->sendResponse(new \Nette\Application\Responses\JsonResponse($response)); } else { $this->sendResponse(new \Nette\Application\Responses\TextResponse('')); } }
public function renderDefault() { // Get server $server = new Lightbulb\Json\Rpc2\Server(); // Bind available functions $server->myTest = new MyTestHandler(); // contains mytest.* methods $server->myFunction = function ($param1, $param2) { /* ... */ }; $server->{'mytesthandler.myfunc'} = array($myObject, 'myMethod'); $server->myStaticHandler = 'MyStaticClass::theStaticFunction'; // Bind events $server->onBeforeCall[] = function ($server) { }; $server->onBeforeCall[] = function ($server) { }; $server->onSuccess[] = function ($server) { }; $server->onError[] = function ($server) { }; // Another way of in-binding the events; it does *not* remove the last one $server->onError = function ($server) { }; // Supress handling of output, as we are sending it differently $server->supressOutput(); // When running the request, we need to check whether there has been anything returned. If not, // then we have to send an empty text response, as Nette\...\JsonResponse throws exception // on empty responses. $response = $server->handle(); if (!empty($response) || is_array($response)) { // is_array in order to send [] $this->sendResponse(new \Nette\Application\Responses\JsonResponse($response)); } else { $this->sendResponse(new \Nette\Application\Responses\TextResponse('')); } }
]', '[ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null} ]', '[ {"jsonrpc": "2.0", "result": 7, "id": "1"}, {"jsonrpc": "2.0", "result": 19, "id": "2"}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ]', ''); /** * Test suite */ require_once __DIR__ . '/../lib/Server.php'; $server = new Lightbulb\Json\Rpc2\Server(); // Bind on* methods //$server->onBeforeCall[] = function() { // echo 'first onBeforeCall called<br />'; //}; //$server->onBeforeCall = function() { // echo 'second onBeforeCall called<br />'; //}; //$server->addCallback('onSuccess', function() { echo 'the action has been successfull'; }); //$server->onError = function() { // echo 'theres been an error'; //}; // Bind request inline method $server->substract = function ($subtrahend, $minuend) { return $subtrahend - $minuend; };
<?php require_once __DIR__ . '/../lib/Server.php'; // Instantiate $server = new Lightbulb\Json\Rpc2\Server(); /** * Available methods: * - notify * - echo * - math.sum * - math2.special.substract */ // Bind methods $server->notify = function () { return 'notify called'; }; $server->echo = function ($what) { return $what; }; class Math { public function sum() { $args = func_get_args(); $result = 0; foreach ($args as $one) { $result += $one; } return $result; } }