1.0.0 • Published 4 years ago
bridgerpc v1.0.0
bridgerpc-js
Bridge RPC client for browser and nodejs. Typescript supported. Documentation
Server-side implement
ASP.Net Core (C#) (server & client): zhshize/BridgeRpcAspNetCore
You can create a server-side implement by yourself, see Node.js server-side implement.
Browser
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;
Node.js
Install the package:
npm i bridgerpc
import:
import BridgeRpc from 'bridgerpc';
Usage
// 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);
});
Nodejs server-side implement
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)
}
}