aardio jsonrpc 服务端和 js jsonrpc 客户端

aardio 服务端示例:

//JSON-RPC服务端

import console;

import process;

import wsock.tcp.simpleHttpServer;

var server = wsock.tcp.simpleHttpServer("127.0.0.1",1428);


console.setTitle("JSON-RPC服务端器已启动",server.getUrl());


 //下面的代码也适用于IIS/FastCGI环境

server.run(

function(response,request){

if( request.path !="/jsonrpc" ){

response.error(404);

}

import console;

if( request.method != 'POST' ){ return ; } //客户端会有2次请求,第一次为 OPTIONS方式的请求,第二次才是POST方式的请求

console.log(request.postData())

import web.rpc.jsonServer;

var jsonServer = web.rpc.jsonServer(); 

//可以定义允许客户端调用的函数

jsonServer.hello = function(name){

/*

第一个返回值为客户端返回值(result),

第二个返回值为错误对象(error)

*/

if(!name) return null,-32602/*_JSONRPC_INVALID_PARAMS*/; 

return "world " + name;

}

//也可以指定允许客户端调用的其他对象

jsonServer.aardio = {

hello = function(){ 

return "aardio.world"

}

err = function(){

error("假装抛出异常试试",2)

}

}

/* 

//下面这种写法也可以

jsonServer.rpc.external = {

hello = function(name){

}

*/

jsonServer.rpc.onError = function(err,requestData){

//未捕获的服务端异常会触发这个函数,客户端错误代码为 _JSONRPC_INTERNAL_ERROR

console.log("发生错误",err,requestData)

}

//运行服务端

jsonServer.rpc.run();

}


simpleHttpServer 需要添加允许跨域的配置:

库代码路径 lib/wsock/tcp/simpleHttpServer.aardio 233行

["open"] = function(){

if( owner._headersWritten ) return; 

owner._headersWritten = true;

var postData = request.postFileData() : request.postData();

owner.status =  owner.status ? ..inet.httpStatusCode.format(owner.status) : "200 OK";

owner.write(environ.SERVER_PROTOCOL," ",owner.status, '\r\n');

owner.write('Access-Control-Allow-Origin:*\r\n');

owner.write('Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n');

owner.write('Access-Control-Allow-Headers: Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, Content-Language, Cache-Control, X-E4M-With\r\n');

owner.write("Content-Type: ",owner.contentType:"text/html","; charset=",owner.charset : "UTF-8", '\r\n');


注意: 跨域的要先于Content-Type部分.


客户端: html + js

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<title></title>

<script type="text/javascript" src="jsonrpcjs-0.1.8.js"></script>

</head>

<body>

<script type="text/javascript">

var rpc = new jsonrpc.JsonRpc('http://127.0.0.1:1428/jsonrpc');


// Handy interceptors for showing loading feedback in the UI

rpc.loading.bind(function(){ console.log('loading...'); });

rpc.loaded.bind(function(){ console.log('done!'); });


// Handy interceptors for all RPC calls that fails and for which there's no failure callback defined

//rpc.unhandledFailure.bind(function(){ console.log('an rpc call failed, and has not failure callback defined'); });


// Simple call style

rpc.call('hello', 'Brain', 2)//, function (result) { console.log('Method aMethod called with hello, Brain. Return value is: ' + result);});


</script>

</body>

</html>


jsonrpcjs库: https://github.com/gimmi/jsonrpcjs

下载zip, cygwin命令行运行 build.cmd 得到 build/jsonrpcjs-0.1.8.js


image.png

image.png