-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1fb470f
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
node-jsonrpc-client | ||
================ | ||
|
||
Simple JSON-RPC client. | ||
|
||
Installation | ||
------------ | ||
|
||
Simply use npm to install it | ||
|
||
npm install jsonrpc-ws | ||
|
||
or download the code from the repo and stick it in your project folder. | ||
|
||
Usage | ||
------------ | ||
|
||
var JSONRPCClient = require('node-jsonrpc-client'); | ||
var client = JSONRPCClient.create("https://my-json-rpc-server/path-to-something/"); | ||
client.call('add', [1, 2], function (result) { | ||
console.log(result); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Simple JSON-RPC Client | ||
*/ | ||
|
||
var http, https, JSONRPCClient, URL_REGEXP; | ||
|
||
// Take url in parts: scheme, host, root zone, port, and path of request | ||
URL_REGEXP = /(http|https):\/\/([\w\-_]+(?:\.([\w\-_]+))+)(?::([\d]+))*([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/; | ||
|
||
http = require('http'); | ||
https = require('https'); | ||
|
||
/** | ||
* JSON-RPC client | ||
* @param {String} scheme Scheme of request (http|https) | ||
* @param {String} host JSON-RPC server | ||
* @param {Number} port Port | ||
* @param {String} path Path of request | ||
*/ | ||
SimpleJSONRPCClient = function (scheme, host, port, path) { | ||
this.scheme = scheme; | ||
this.host = host; | ||
this.port = port; | ||
this.path = path; | ||
|
||
/** | ||
* Call method of JSON-RPC API | ||
* @param {String} method Method | ||
* @param {Array} params Params of request | ||
* @param {Function} [onSuccessCallback] Callback for success result | ||
* @param {Function} [onErrorCallback] Callback for error | ||
*/ | ||
this.call = function(method, params, onSuccessCallback, onErrorCallback) { | ||
var requestJSON, requestOptions, request; | ||
|
||
requestJSON = JSON.stringify({ | ||
'id': '' + (new Date()).getTime(), | ||
'method': method, | ||
'params': params | ||
}); | ||
|
||
requestOptions = { | ||
host: this.host, | ||
port: this.port, | ||
path: this.path, | ||
method: 'POST', | ||
headers: { | ||
'Content-Length': requestJSON.length | ||
} | ||
} | ||
|
||
// Send request | ||
request = require(this.scheme).request(requestOptions, onComplete); | ||
request.write(requestJSON); | ||
request.end(); | ||
|
||
function onComplete (res) { | ||
var buffer = ''; | ||
|
||
res.setEncoding('utf8'); | ||
|
||
res.on('data', function(chunk) { | ||
buffer = buffer + chunk; | ||
}); | ||
|
||
res.on('end', function () { | ||
var decoded = JSON.parse(buffer); | ||
if(decoded.result) { | ||
onSuccessCallback && onSuccessCallback(decoded.result); | ||
} else { | ||
onErrorCallback && onErrorCallback(decoded.error); | ||
} | ||
}); | ||
|
||
res.on('error', function (error) { | ||
onErrorCallback && onErrorCallback(error); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = { | ||
|
||
/** | ||
* Create JSON-RPC client | ||
* @param {String} url Server url | ||
* @return {JSONRPCClient} client | ||
*/ | ||
create: function (url) { | ||
var urlParts = url.match(URL_REGEXP), | ||
host = urlParts[2], | ||
port = urlParts[4] || urlParts[1] === 'https' ? 443 : 8080, | ||
path = urlParts[5], | ||
scheme = urlParts[1] || 'http'; | ||
|
||
return new JSONRPCClient(scheme, host, port, path); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name" : "jsonrpc-client", | ||
"version" : "0.0.1", | ||
"description" : "A simple JSON-RPC client.", | ||
"author" : "Alexander Tarmolov <alexander@tarmolov.ru>", | ||
"main" : "./jsonrpc-client.js", | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "http://github.com/tarmolov/node-jsonrpc-client" | ||
}, | ||
"licenses" : [ | ||
{ | ||
"type": "AS IS" | ||
} | ||
], | ||
"engine" : { "node" : ">=0.2.0" } | ||
} |