Please provide an example of client JSON-RPC

RCF support and general discussion.
Post Reply
wulixue
Posts: 8
Joined: Sun Dec 20, 2015 6:26 pm

Please provide an example of client JSON-RPC

Post by wulixue »

HI, I have used guide JSON-RPC server example.

// JSON-RPC.cpp 。
//

#include "stdafx.h"
#include <string>
#include <RCF/RCF.hpp>
#include <RCF/JsonRpc.hpp>
#include <RCF/json_spirit.h>
#include <SF/string.hpp>

#include <iostream>

#pragma comment(lib,"RCF.lib")

RCF_BEGIN(I_HelloWorld, "I_HelloWorld")
RCF_METHOD_V1(void, Print, const std::string &)
RCF_END(I_HelloWorld)

class HelloWorldImpl
{
public:
void Print(const std::string & s)
{
std::cout << "I_HelloWorld service: " << s << std::endl;
};

void JsonPrint(const RCF::JsonRpcRequest &request,RCF::JsonRpcResponse &response)
{
// Print out all the strings passed in, and return the number of
// characters printed.

int charsPrinted = 0;

const json_spirit::Array &params = request.getJsonParams();
for (std::size_t i = 0; i<params.size(); ++i)
{
const std::string & s = params.get_str();
std::cout << "I_HelloWorld service: " << s << std::endl;
charsPrinted += s.size();
}

// Return number of characters printed.
json_spirit::mObject &responseObj = response.getJsonResponse();
responseObj["result"] = charsPrinted;
};
};


int _tmain(int argc, _TCHAR* argv[])
{
RCF::RcfInitDeinit rcfInit;
RCF::enableLogging(RCF::LogToDebugWindow(), 4);
RCF::RcfServer server;

// Accept RCF client requests on port 50001.
HelloWorldImpl helloWorld;
server.bind<I_HelloWorld>(helloWorld);
server.addEndpoint(RCF::TcpEndpoint(50001));

// Accept JSON-RPC requests over HTTP on port 80.
server.bindJsonRpc(boost::bind(&HelloWorldImpl::JsonPrint, &helloWorld, _1, _2),"JsonPrint");

server.addEndpoint(RCF::HttpEndpoint(80)).setRpcProtocol(RCF::Rp_JsonRpc);

server.start();

// RCF clients can call Print() on port 50001.
// ...

// JSON-RPC clients can call JsonPrint() over HTTP on port 80.
// ...

while (true)
{
Sleep(5000);
}

return 0;

}

I tried to write several client code,but all is fail, example.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jq ... "></script>
<link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />
<script type="text/javascript">
$.post(
"http://127.0.0.1/index.json",
{"jsonrpc": "2.0", "method": "JsonPrint", "params": [{"name": "Nick"}], "id": 1},
function(data){
alert("Data Loaded: " + data);
},
"jsonp"
);
</script>

</head>
<body>
<h1 id="qunit-header">jQuery JSON RPC 2.0 plugin specs</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>
</body>
</html>

how to write a correct client code.

thanks.

Post Reply