已贬值
从2024年9月23日起, micro-rpc-client 将无法维持 并堕落.
微端端端客户端
通用JS RPC客户端 https://github.com/bufferapp/buffer-rpc/ 页面存档备份,存于互联网档案馆
快速启动
创建 RPC 客户端并调用添加函数
const RPCClient = require('micro-rpc-client');
const client = new RPCClient({
url: 'https://localhost:3000/rpc',
});
const main = async () => {
const result = await client.call('add', [1, 2]);
console.log(result); // 3
};
main();
使用量
如何调用客户端方法的几个实例
const RPCClient = require('micro-rpc-client');
const client = new RPCClient({
url: 'https://localhost:3000/rpc',
sendCredentials: 'same-origin', // send cookies on same origin requests
});
const main = async () => {
const methods = await client.listMethods();
console.log(method);
/*
[
{
"docs": "add two numbers"
"name": "add"
},
{
"docs": "list all available methods",
"name": "methods"
}
]
*/
const result = await client.call('add', [1, 2]);
console.log(result); // 3
};
main();
API 密码
构造器
创建 RPC 客户端实例
const client = new RPCClient({
url,
sendCredentials,
});
url 密码 - 说吧 字符串 - RPC 端点服务器的完整url
发送证书 - 说吧 字符串 - 当发送像饼干一样的东西 与请求, 传递参数到证书 init 参数 在抓取 API https://developer.mozila.org/en-US/docs/Web/API/WindowOrWorker GlobalScope/fetch#Paramels (英语)
返回 RPC 客户端实例
列表 方法
在服务器上列出所有远程方法
const methods = await client.listMethods()
返回以远程方法列表解析的许诺
调用
const result = await client.call(name, args)
名称 - 说吧 字符串 - 远程 RPC 端点名称
参数 - 说吧 数组 或者说 对象 - 传递到远程 RPC 端点的参数
返回一个承诺,以结果解决,以错误拒绝.
处理错误
当调用远程函数时 call 答复有不同类别:
成绩
这是典型的成功案例
statusCode = 200
result = {} // JSON
失败( 手持)
从 创建错误数字 :
statusCode = 400
result = {
error: 'string',
code: 1000, // or some custom code
handled: true,
}
失败( 未处理)
从 中间软件出错数字 :
statusCode = 500
result = {
error: 'string',
code: 1000, // or some custom code
handled: true,
}
失败( 意外)
如果 中间软件出错 尚未连接, 或者发生了出乎意料的事情 .