simple but effective RPC framework. Both server and client.
npm i @mini-rpc/server
# or
pnpm add @mini-rpc/server
# or
yarn add @mini-rpc/server
tsconfig.json
in server
{
"compilerOptions": {
"target": "ES2018",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
}
}
- install
reflect-metadata
npm i reflect-metadata
# or
pnpm add reflect-metadata
# or
yarn add reflect-metadata
- import
reflect-metadata
on entry (e.g:index.ts
)
import { Service, Callable } from "@mini-rpc/server";
@Service()
class MyService {
@Callable()
add(...nums: number[]) {
return nums.reduce((acc, cur) => acc + cur);
}
}
const server = new RPCServer();
server.registerServices([MyService]);
server.listen(3000);
/*
* you need to install `@mini-rpc/client` on the client side
*/
import { RPCClient } from "@mini-rpc/client";
const client = new RPCClient("ws://localhost:3000");
client.connect();
client
.call("MyService.add", [1,2,3])
.then((result) => {
console.log(result); // 6
})
.catch((err) => {
console.error(err);
});
- the
@Callable
method can be asynchronous.
class x {
//...
@Callable()
async add(a:number,b:number) {
// asynchronous code
}
// or declare the return type Promise
@Callable()
promiseAdd(a:number,b:number):Promise<number> {
return Promise<number>()
}
}