Browse Source

Added better JSON-RPC example

master
Artyom Beilis 12 years ago
parent
commit
6769a9e1c6
5 changed files with 93 additions and 5 deletions
  1. +2
    -0
      contrib/client_side/jsonrpc/jsonrpc.js
  2. +9
    -1
      examples/json_rpc/config.js
  3. +7
    -3
      examples/json_rpc/index.html
  4. +3
    -1
      examples/json_rpc/rpc.cpp
  5. +72
    -0
      examples/json_rpc/usingrpc.html

+ 2
- 0
contrib/client_side/jsonrpc/jsonrpc.js View File

@@ -25,6 +25,8 @@
/// and it should be some integer or string for function methods
///
function JsonRPC(uri,function_methods,notification_methods) {
if(!(this instanceof JsonRPC))
return new JsonRPC(uri,function_methods,notification_methods);
this.uri = uri;
if(typeof function_methods != 'undefined') {
for(var i=0;i<function_methods.length;i++)


+ 9
- 1
examples/json_rpc/config.js View File

@@ -6,6 +6,14 @@
"http" : {
"script" : "/rpc"
},
"file_server" : { "enable" : true }
"file_server" : {
"enable" : true,
"alias" : [
{
"url" : "/scripts" ,
"path" : "../../contrib/client_side/jsonrpc"
}
]
}
}


+ 7
- 3
examples/json_rpc/index.html View File

@@ -9,13 +9,13 @@ function call() {
xhr.setRequestHeader("Content-Type","application/json");

// It is better to use real formatter like JSON.js
x=parseInt(document.getElementById('x').value);
y=parseInt(document.getElementById('y').value);
var x=parseInt(document.getElementById('x').value);
var y=parseInt(document.getElementById('y').value);
var request = '{"method":"div","params":[' + x + ',' + y +'],"id":1}';

xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
res = 'Unknown Error';
var res;
if(xhr.status === 200) {
// Don't call eval in real code use some parser
var result = eval('(' + xhr.responseText + ')');
@@ -26,6 +26,9 @@ function call() {
res = result.error;
}
}
else {
res = 'Invalid Status ' + xhr.status;
}
document.getElementById('result').innerHTML = res;
}
}
@@ -33,6 +36,7 @@ function call() {
return false;
}
</script>
<p><a href="usingrpc.html">Using jsonrpc.js</a></p>
<form onsubmit="return call();">
<p>
<input type="text" id="x" />


+ 3
- 1
examples/json_rpc/rpc.cpp View File

@@ -10,15 +10,17 @@ public:
{
bind("sum",cppcms::rpc::json_method(&json_service::sum,this),method_role);
bind("div",cppcms::rpc::json_method(&json_service::div,this),method_role);
bind("notify",cppcms::rpc::json_method(&json_service::div,this),notification_role);
bind("notify",cppcms::rpc::json_method(&json_service::notify,this),notification_role);
bind("both",cppcms::rpc::json_method(&json_service::both,this));
}
void sum(int x,int y)
{
std::cout << "Sum Called" << std::endl;
return_result(x+y);
}
void div(int x,int y)
{
std::cout << "Div Called" << std::endl;
if(y==0)
return_error("Division by zero");
else


+ 72
- 0
examples/json_rpc/usingrpc.html View File

@@ -0,0 +1,72 @@
<html>
<head>
<script type="text/javascript" src="/scripts/json2.js"></script>
<script type="text/javascript" src="/scripts/jsonrpc.js"></script>
</head>
<body>
<script type="text/javascript">

var rpc = new JsonRPC('/rpc',['div','sum'], ['notify']);

function getX()
{
return parseInt(document.getElementById('x').value);
}
function getY()
{
return parseInt(document.getElementById('y').value);
}

function setResult(r)
{
document.getElementById('result').innerHTML += r + '<br>';
}

function call()
{
var x = getX();
var y = getY();
// Synchronous call
/*
try {
setResult(x+'/'+y+ '=' + rpc.div(x,y));
setResult(x+'+'+y+ '=' + rpc.sum(x,y));
rpc.notify('test');
}
catch(e) {
setResult(e.message);
}
*/

// Asynchronous call
rpc.div.on_error = function(e) { setResult(e.error); }
rpc.div.on_result = function(r) { setResult(x + '/' + y + '=' + r ); }
rpc.sum.on_error = function(e) { setResult(e.error); }
rpc.sum.on_result = function(r) { setResult(x + '+' + y + '=' + r); }

rpc.notify.on_error = function() { setResult('Notification error:' + e.error); }
rpc.notify.on_result = function() { setResult('Notification Called'); }
rpc.div(x,y);
rpc.sum(x,y);
rpc.notify('test');
return false;
}
</script>
<p><a href="/">Using plain XMLHttpRequest</a></p>
<form onsubmit="return call();">
<p>
<input type="text" id="x" />
<input type="submit" value="?" />
<input type="text" id="y" /> =
</p>
<p id="result"></p>
</form>
</body>
</html>


Loading…
Cancel
Save