vsDizzy / rpc-lib

Custom RPC library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rpc-lib

Two-way RPC over websocket. The main goal is to expose entire interfaces not individual methods.

Usage:

import * as rpc from 'rpc-lib';

socket server

export class ServerApi {
  add(a: number, b: number) {
    return a + b;
  }

  sub(a: number, b: number) {
    return a - b;
  }
}
const rh = new rpc.Host(socket);
rh.addEndpoint('server-api', new ServerApi());

socket client

  const rh = new rpc.Host(socket);

  const serverApi = rh.getEndpoint<ServerApi>('server-api');
  console.log('1 + 2 =', await serverApi.add(1, 2));
  console.log('1 - 2 =', await serverApi.sub(1, 2));
});

About

Custom RPC library


Languages

Language:TypeScript 98.8%Language:JavaScript 1.2%