Maus
A Tiny RPC Framework running in NodeJS or Browser
Install
npm install maus --save
QuickStart
worker.js
var rpcWorker = worker;rpcWorker;
node worker.js
Or you can webpack it and run it in browser!!!
manager.js
var rpcManager = manager; var myManager = 8124; myManager
node manager.js
Usage
1、Worker
Worker.create(methodObject, path)
- methodObject:
The methodObject contains some methods for RPC. Methods must return a value or a Promise.
Support return type:
Number
, NaN
, Infinity
, Error
, Undefined
, String
, Array
, Boolean
, Date
, RegExp
, Object
Please use Async
as suffix for some async method, like httpGetAsync
、readFileAsync
、somePromiseAsync
:
x + y { return { } }
- path
The path of Manager
Worker.registerParkserver(path, workerType, methodObject)
- path
The path of Parkserver
- workerType
The type of worker, default value is "default"
- methodObject
Same as methodObject
in Worker.create(methodObject, path)
.
rpcWorker
2、Manager
Manager(port)
- port
The port that Manager listens to
var myManager = 8124;
Manager.do(callback)
- callback
The callback function will be executed after init. It will gets a workers static
as arguments, which contains all the methods in Worker
Manager;
The params of methods in Worker
can be a Number
, Object
, String
, Array
, or even Function
. Please use __this
as the function itself in any recursion algorithm, such as:
var x > 1 ? + : x;
Manager.connectParkserver(path)
- path
The path of Parkserver
Manager;
Manager.getWorker(config)
This method should be called after Manager.connectParkserver
.
config
has 3 attributes:
amount
[number] optional:
The amount of workers that you want to get. if this is not defined, the Manager will get all the available workers.
workerType
[string] optional:
The type of workers that you want to get. The default value is "default"
.
address
[string] required: The address of Manager
Manager;Manager;
Manager.end
Release connected workers. Those workers will be reassigned by Parkserver.
3、Parkserver
Parkerver is an optional part of Maus. It will monitor working status of workers, assign suitable workers to Manager. You can use it to handle multiple Managers if needed.
Manager can get workers it wants through parkserver. If the Manager is died, the workers that assigned to it will be recollected by parkserver, and can be reassigned to other Managers.
Parkserver(port)
- port
The port that Parkserver listens to
var parkserver = parkserver;var myParkserver = 8500;