dscmall/laravel-json-rpc

Json Rpc 包

v2.1.0 2020-09-10 06:01 UTC

This package is auto-updated.

Last update: 2024-08-24 16:45:53 UTC


README

logo

JSON-RPC 2.0

Version PHP Version Build Status Coverage Downloads count License

JSON-RPC 2.0 是一种用 JSON 编码的远程过程调用协议。这是一个非常简单的协议,仅定义了少量数据类型和命令。JSON-RPC 允许发送通知(发送到服务器而不需要响应的数据)以及发送多个调用到服务器,这些调用可能以不同的顺序得到响应。

安装

使用以下命令通过 composer 安装此包:

$ composer require dscmall/laravel-json-rpc "^2.0"

需要安装 composer如何安装 composer)。

您需要修复包的主版本号。

使用示例

创建路由

使用 RpcRouter 门面在 ./routes/web.php 中注册您的方法的操作

<?php

use AvtoDev\JsonRPC\RpcRouter;

RpcRouter::on('please_sum_array_values', 'YourNamespace\\SomeController@sum');
RpcRouter::on('show_full_request', 'YourNamespace\\SomeController@showInfo');

此包已经包含一个简单的控制器实现,您可以根据需要扩展它或仅将其作为示例。

RpcController 添加新路由

<?php

use Illuminate\Support\Facades\Route;

Route::post('/rpc', 'AvtoDev\\JsonRpc\\Http\\Controllers\\RpcController');

如果您想指定不在文件 ./routes/web.php 中的路由,请务必加载 RPC 路由

创建 RPC 控制器

创建一个新的控制器,包含通过 JSON 请求调用的过程

<?php

namespace YourNamespace;

use AvtoDev\JsonRpc\Requests\RequestInterface;

class SomeController
{
    /**
     * Get sum of array.
     *
     * @param RequestInterface $request
     *
     * @return int
     */
    public function sum(RequestInterface $request): int
    {
        return (int) \array_sum($request->getParams());
    }

    /**
     * Get info from request.
     *
     * @param RequestInterface $request
     *
     * @return mixed[]
     */
    public function showInfo(RequestInterface $request): array
    {
        return [
            'params'       => $request->getParams(),
            'notification' => $request->isNotification(),
            'method_name'  => $request->getMethod(),
            'request ID'   => $request->getId(),
        ];
    }
}

发送 RPC-请求

$ curl 'https:///rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"please_sum_array_values","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'

{
    "jsonrpc": "2.0",
    "result": 55,
    "id": "UNIQ_REQUEST_ID"
}
$ curl 'https:///rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"show_full_request","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'

{
    "jsonrpc": "2.0",
    "result": {
        "params": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "notification": false,
        "method_name": "please_sum_array_values",
        "request ID": "UNIQ_REQUEST_ID"
    },
    "id": "UNIQ_REQUEST_ID"
}
$ curl 'https:///rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"undefined_method","id":"UNIQ_REQUEST_ID"}'

{
    "jsonrpc": "2.0",
    "error": {
        "code": -32601,
        "message": "Method not found"
    },
    "id": "UNIQ_REQUEST_ID"
}

事件

当调用 Kernel@handle 方法时,会触发一些事件

如果需要,您可以在该 事件 上创建一个 监听器 并显示收到的消息以进行调试。

您的 Laravel 应用程序 中包含的 EventServiceProvider 为您注册应用程序的所有事件监听器提供了一个方便的地方。

变更日志

Release date Commits since latest release

变更日志可以在 此处找到

支持

Issues Issues

如果您在此包中找到任何错误,请在此当前存储库中 创建一个问题

许可

这是一个开源软件,根据 MIT 许可证 许可。