Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a REPL to multitransport-jsonrpc so you can easily connect to …

…a server and test the remote methods from the command line.
  • Loading branch information...
commit 9171649d851b31d136d5e77374ece6cfe27299a4 1 parent a684860
@dfellis dfellis authored
Showing with 51 additions and 2 deletions.
  1. +19 −1 README.md
  2. +27 −0 bin/jsonrpc-repl
  3. +5 −1 package.json
View
20 README.md
@@ -19,7 +19,11 @@ The TCP transport is nearly 25x faster for small messages than for large.
npm install multitransport-jsonrpc
-## Usage
+If you want to use the ``jsonrpc-repl`` binary, also
+
+ npm install -g multitransport-jsonrpc
+
+## Library Usage
```js
var jsonrpc = require('multitransport-jsonrpc'); // Get the multitransport JSON-RPC suite
@@ -260,6 +264,20 @@ On the client side, you can only use the methods in an asynchronous way. All ass
jsonRpcClient.request("shutdown", ["arg1", "arg2"], callbackFunc);
```
+# Using the jsonrpc-repl binary
+
+ Usage: jsonrpc-repl [options]
+
+ Options:
+
+ -h, --help output usage information
+ -s, --server <hostname> The hostname the server is located on. (Default: "localhost")
+ -p, --port <portnumber> The port the server is bound to. (Default: 80)
+ -t, --tcp Connects to the server via TCP instead of HTTP (Default: false)
+
+The ``jsonrpc-repl`` dumps you into a [Node.js repl](http://nodejs.org/api/repl.html) with some bootstrapping done on connecting you to the RPC server and getting a list of valid server methods. You can access them with the ``rpc`` object in the exact same way as described above in the "Using JSON-RPC Client Methods" section.
+
+
## Creating A New Transport
If you want to write your own transport constructor functions for multitransport-jsonrpc, here's what the client and server objects expect from their transport:
View
27 bin/jsonrpc-repl
@@ -0,0 +1,27 @@
+#!/usr/bin/env node
+
+var repl = require('repl');
+var commander = require('commander');
+var l = require('lambda-js');
+var jsonrpc = require('../lib/index');
+var Client = jsonrpc.client;
+var TcpTransport = jsonrpc.transports.client.tcp;
+var HttpTransport = jsonrpc.transports.client.http;
+
+commander
+ .option('-s, --server <hostname>', 'The hostname the server is located on. (Default: "localhost")', 'localhost')
+ .option('-p, --port <portnumber>', 'The port the server is bound to. (Default: 80)', 80)
+ .option('-t, --tcp', 'Connects to the server via TCP instead of HTTP (Default: false)', false)
+ .parse(process.argv);
+
+var Transport = commander.tcp ? TcpTransport : HttpTransport;
+
+new Client(new Transport(commander.server, commander.port), {}, function(client) {
+ console.log('Connected to ' + commander.server + ':' + commander.port + ' over ' + (commander.tcp ? 'TCP' : 'HTTP'));
+ console.log('The server reported the following methods:');
+ console.log(Object.getOwnPropertyNames(client).filter(l('name', 'name !== "transport"')).join(', '));
+ console.log('Access them with `rpc.foo(arg1, arg2, callbackFunction)`');
+ var r = repl.start('jsonrpc> ');
+ r.context.rpc = client;
+ r.on('exit', process.exit.bind(process));
+});
View
6 package.json
@@ -15,9 +15,13 @@
"url": "git://git@github.com:uber/multitransport-jsonrpc.git"
},
"main": "lib/index.js",
+ "bin": {
+ "jsonrpc-repl": "bin/jsonrpc-repl"
+ },
"dependencies": {
"queue-flow": "*",
- "lambda-js": "*"
+ "lambda-js": "*",
+ "commander": "*"
},
"devDependencies": {
"nodeunit": "*",
Please sign in to comment.
Something went wrong with that request. Please try again.