Bridge RPC client for browser and nodejs. Typescript supported. Documentation
ASP.Net Core (C#) (server & client): zhshize/BridgeRpcAspNetCore
You can create a server-side implement by yourself, see Node.js server-side implement.
bridgerpc-js needs msgpack-lite. You can broserify it or use CDN:
<script src="https://cdnjs.cloudflare.com/ajax/libs/msgpack-lite/0.1.26/msgpack.min.js" integrity="sha256-xnDLLYKxKFwLEmQK1SkZ9I7IwmjdeURGtXUk/0WnTRo=" crossorigin="anonymous"></script>
And then, include bridgerpc:
<script src="path/to/bridgerpc.umd.js"></script>
BridgeRpc UMD module is exposed on the global variable bridgerpc, contains
RpcResonse, RpcRequest, RpcError, and default actually is BridgeRpc.
If you want to expose all prototype, the code below is useful:
const RpcResonse = bridgerpc.RpcResonse;
const RpcRequest = bridgerpc.RpcRequest;
const RpcError = bridgerpc.RpcError;
const BridgeRpc = bridgerpc.default;
Install the package:
npm i bridgerpcimport:
import BridgeRpc from 'bridgerpc';
// Initialize a client. but not connect until calling connect()
var client = new BridgeRpc("ws://localhost/");
// Registering a request handler for method 'echo'
client.onRequest("echo", request => {
return request.data;
// If you didn't return a RpcResponse object, the return value will be set as
// result of a new RpcRsponse object.
// Or you can return a RpcResponse object:
// var r = new RpcResponse();
// r.result = request.data;
// return r;
});
// Registering a notification handler for method 'notify'
client.onNotify("notfiy", request => {
console.log(request.data);
});
// Some staff when connected
client.onConnect(async () => {
// Requesting server-side
const res = await client.request("greet", "Joe");
console.log(res.result);
});
I suggest you using Typescript to obtain more flexibility of customizing BridgeRpc.
import BridgeRpc from 'bridgeRpc'
class RpcConnection extends BridgeRpc {
connect() {
// Hack HERE! Replace it to your WebSocket object (by adding a setter) from
// server (i.e. express.js). The object you passed must be suitable for
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
this._rawSocket = new WebSocket()
this._rawSocket.binaryType = 'arraybuffer'
this._rawSocket.onmessage = this.onMessage.bind(this)
}
}
RPC Notification handler must process a request, nothing should be returned.
Generated using TypeDoc
RPC Request handler must process a request and return a RpcResponse.