Re: [Cppcms-users] JSON-RPC dispatching together with Normal Web Page
Brought to you by:
artyom-beilis
|
From: Varstray Pl <var...@gm...> - 2020-11-09 17:29:43
|
Ah! Ok. So this is actually very helpful!
I've decided to go with mounting the rpc server in the webcomic's url space
using the attach method you described above. The code for my webcomic page
constructor is as follows:
* * *
webcomic::webcomic(cppcms::service &srv) : cppcms::application(srv){
attach(new json_service(srv), "rpc", "/rpc{1}", "/rpc(/(.*))?", 1);
//.. various dispatcher and mapper calls.
};
***
The browser now appears to be successfully calling the rpc server! Yay!
Unfortunately (there's always an unfortunately, isn't there? xD), I think I
haven't set up the handling of the rpc request properly within the server,
because instead of a proper response, the widgets panel is simply filled
with the text "Invalid JSON-RPC". I'm not sure where in the pipeline things
are going wrong, but here is how I've set up the rpc server. Maybe you can
see what I'm doing wrong?
First, the basic class definition:
* * *
class json_service: public cppcms::rpc::json_rpc_server{
public:
json_service(cppcms::service &srv);
// Ajax Methods
void widgets();
};
* * *
And then the implementation of the server constructor and the widgets
method:
* * *
json_service::json_service(cppcms::service &srv) :
cppcms::rpc::json_rpc_server(srv)
{
bind("widgets", cppcms::rpc::json_method(&json_service::widgets,
this),method_role);
}
void json_service::widgets()
{
return_result("widgets via AJAX-RPC!!! Cool!");
}
* * *
Finally, this is how the request is set up on the client side via
javascript:
* * *
function getWidgets() {
var xhr = new XMLHttpRequest();
xhr.open("post", '/rpc');
// Required by JSON-RPC over HTTP
xhr.setRequestHeader("Content-Type","application/json");
// Configure JSON-RPC request.
var request = '{"method":"widgets"}';
// Define our callback function.
xhr.onreadystatechange = function(){
if (xhr.readyState === 4){
var response;
if (xhr.status === 200){
response = xhr.responseText;
} else {
response = 'Invalid Status: ' + xhr.status;
}
document.getElementById('widgets').innerHTML = response;
}
}
xhr.send(request);
return false;
}
* * *
The javascript method is pretty much taken from the json-rpc tutorial on
the cppcms.com website. Although because this method doesn't take any
parameters(yet), I *think* perhaps the issue lies somewhere in the
modifications I made for sending a request for a non-parameter method,
whereas the tutorial method does take parameters. Also, up till this point,
diagnostic information on stderr and stdout has been very helpful, but now
there doesn't appear to be any stderr output at all, lol.
I *can* verify that on the browser end the rpc call is actually being sent
to the rpc server, and because 'Invalid RPC-JSON' is different from
'Invalid Status: bla bla', I think that the getWidgets() method is actually
getting a proper response from the rpc server. It's just clearly there's
something else going wrong somewhere.
* * *
Thank you for your help, Jon, and sorry for any additional trouble. I'll
also keep searching on my end for an answer as well. ^^;
|