Options
All
  • Public
  • Public/Protected
  • All
Menu

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

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;

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)
  }
}

Index

Type aliases

RpcHandler

RpcHandler: function

RPC Request handler must process a request and return a RpcResponse.

Type declaration

RpcNotificationHandler

RpcNotificationHandler: function

RPC Notification handler must process a request, nothing should be returned.

Type declaration

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc