您现在的位置是:首页-> 米鼠技术 ->Ajax 方式实现的 PHPRPC 2.1 客户端

Ajax 方式实现的 PHPRPC 2.1 客户端

本文给出了 PHPRPC 2.1 协议的 Ajax 客户端实现。该客户端的使用方式与 2.0 版本的使用方式基本上相同。不同点在于如果要调用的远程函数是引用参数传递的话,需要在调用前设置该函数的 ref 属性为 true。该属性表示是否是引用参数传递。该属性默认为 false。推荐在客户端对象的 onready 事件中进行设置。

下面为该客户端的实现代码:

下载: phprpc_ajax_client.js
  1. /* phprpc_ajax_client.js - Ajax PHPRPC Client
  2. *
  3. * Copyright Ma Bingyao <andot@ujn.edu.cn>
  4. * Version: 2.1
  5. * LastModified: 2006-03-12
  6. * This library is free.  You can redistribute it and/or modify it.
  7. */
  8.  
  9. /*
  10. * Interfaces:
  11. * phprpc_client.create('rpc');
  12. * rpc.method_callback = function(result, args, output) {
  13. *     if (result instanceof phprpc_error) {
  14. *         alert(result.errstr);
  15. *     }
  16. *     else {
  17. *         alert(result);  // or do any other things.
  18. *     }
  19. * }
  20. * ....
  21. * if (rpc.ready) rpc.method();
  22. */
  23.  
  24. function phprpc_error(errno, errstr) {
  25.     this.errno = errno;
  26.     this.errstr = errstr;
  27. }
  28.  
  29. function phprpc_client() {
  30.     this.__php = new PHP_Serializer();
  31.     this.__url = '';
  32.     this.__encrypt = false;
  33.     this.encrypt = 0;
  34.     this.async = true;
  35.     this.ready = false;
  36.     this.args = null;
  37.     this.output = "";
  38.     this.use_service = function (url, encrypt) {
  39.         if (typeof(encrypt) == "undefined") {
  40.             encrypt = this.__encrypt;
  41.         }
  42.         if (typeof(this.__name) == "undefined") {
  43.             return false;
  44.         }
  45.         this.__url = url;
  46.         var xmlhttp = this.__create_xmlhttp();
  47.         var __rpc = this;
  48.         if (encrypt === true) {
  49.             xmlhttp.onreadystatechange = function () {
  50.                 if (xmlhttp.readyState == 4) {
  51.                     if (xmlhttp.responseText) {
  52.                         eval(xmlhttp.responseText);
  53.                         if (typeof(phprpc_encrypt) == "undefined") {
  54.                             __rpc.__encrypt = false;
  55.                             __rpc.use_service(__rpc.__url);
  56.                         }
  57.                         else {
  58.                             __rpc.__encrypt = __rpc.__php.unserialize(phprpc_encrypt);
  59.                             __rpc.__encrypt['p'] = dec2num(__rpc.__encrypt['p']);
  60.                             __rpc.__encrypt['g'] = dec2num(__rpc.__encrypt['g']);
  61.                             __rpc.__encrypt['y'] = dec2num(__rpc.__encrypt['y']);
  62.                             __rpc.__encrypt['x'] = rand(127, 1);
  63.                             var key = pow_mod(__rpc.__encrypt['y'],
  64.                                               __rpc.__encrypt['x'],
  65.                                               __rpc.__encrypt['p']);
  66.                             key = num2str(key);
  67.                             for (var i = 0; i < 16 - key.length; i++) {
  68.                                 key = '\0' + key;
  69.                             }
  70.                             __rpc.__encrypt['k'] = key;
  71.                             var encrypt = pow_mod(__rpc.__encrypt['g'],
  72.                                                   __rpc.__encrypt['x'],
  73.                                                   __rpc.__encrypt['p']);
  74.                             __rpc.use_service(__rpc.__url, num2dec(encrypt).replace(/\+/g, '%2B'));
  75.                         }
  76.                     }
  77.                     delete(xmlhttp);
  78.                 }
  79.             }
  80.         }
  81.         else {
  82.             xmlhttp.onreadystatechange = function () {
  83.                 if (xmlhttp.readyState == 4) {
  84.                     if (xmlhttp.responseText) {
  85.                         eval(xmlhttp.responseText);
  86.                         var functions = __rpc.__php.unserialize(phprpc_functions);
  87.                         var func;
  88.                         for (var i = 0; i < functions.length; i++) {
  89.                             func = __rpc.__name + "." + functions[i] + " = function () {\r\n";
  90.                             func += "    this.__call('"  + functions[i] + "', this.__args_to_array(arguments));\r\n";
  91.                             func += "}\r\n";
  92.                             func += __rpc.__name + "." + functions[i] + ".ref = false;\r\n";
  93.                             eval(func);
  94.                         }
  95.                         __rpc.ready = true;
  96.                         if (typeof(__rpc.onready) == "function") {
  97.                             __rpc.onready();
  98.                         }
  99.                     }
  100.                     delete(xmlhttp);
  101.                 }
  102.             }
  103.         }
  104.         xmlhttp.open("get", this.__url + '?phprpc_encrypt=' + encrypt + '&phprpc_encode=false', true);
  105.         xmlhttp.send(null);
  106.     };
  107.     this.__call = function (func, args) {
  108.         var __args = this.__php.serialize(args);
  109.         if ((this.__encrypt !== false) && (this.encrypt > 0)) {
  110.             __args = xxtea_encrypt(__args, this.__encrypt['k']);
  111.         }
  112.         __args = base64encode(__args);
  113.         var request = 'phprpc_func=' + func
  114.                     + '&phprpc_args=' + __args
  115.                     + '&phprpc_encode=false'
  116.                     + '&phprpc_encrypt=' + this.encrypt;
  117.         var ref = eval(this.__name + "." + func + ".ref");
  118.         if (!ref) {
  119.             request += '&phprpc_ref=false';
  120.         }
  121.         var xmlhttp = this.__create_xmlhttp();
  122.         var session = {'args': args, 'ref': ref, 'encrypt': this.encrypt};
  123.         if (this.async) {
  124.             var __rpc = this;
  125.             xmlhttp.onreadystatechange = function () {
  126.                 if (xmlhttp.readyState == 4) {
  127.                     if (xmlhttp.responseText) {
  128.                         __rpc.__get_result(xmlhttp, session);
  129.                         if (typeof(eval(__rpc.__name + "." + func + "_callback")) == "function") {
  130.                             eval(__rpc.__name + "." + func + "_callback(phprpc_result, phprpc_args, phprpc_output);");
  131.                         }
  132.                     }
  133.                     delete(xmlhttp);
  134.                 }
  135.             }
  136.         }
  137.         xmlhttp.open("post", this.__url, this.async);
  138.         xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  139.         xmlhttp.send(request.replace(/\+/g, '%2B'));
  140.         if (!this.async) {
  141.             if (xmlhttp.responseText) {
  142.                 this.__get_result(xmlhttp, session);
  143.                 this.output = phprpc_output;
  144.                 this.args = phprpc_args;
  145.                 return phprpc_result;
  146.             }
  147.             else {
  148.                 return new phprpc_error(1, "No data received from server");
  149.             }
  150.             delete(xmlhttp);
  151.         }
  152.     };
  153.     this.__get_result = function (xmlhttp, session) {
  154.         eval(xmlhttp.responseText);
  155.         if (phprpc_errno == 0) {
  156.             if ((this.__encrypt !== false) && (session.encrypt > 0)) {
  157.                 if (session.encrypt > 1) {
  158.                     phprpc_result = xxtea_decrypt(phprpc_result, this.__encrypt['k']);
  159.                 }
  160.                 if (session.ref) {
  161.                     phprpc_args = xxtea_decrypt(phprpc_args, this.__encrypt['k']);
  162.                 }
  163.             }
  164.             phprpc_result = this.__php.unserialize(phprpc_result);
  165.             if (session.ref) {
  166.                 phprpc_args = this.__php.unserialize(phprpc_args);
  167.             }
  168.             else {
  169.                 phprpc_args = this.args;
  170.             }
  171.         }
  172.         else {
  173.             phprpc_result = new phprpc_error(phprpc_errno, phprpc_errstr);
  174.             phprpc_args = null;
  175.         }
  176.     }
  177.     // the function __create_xmlhttp modified from
  178.     this.__create_xmlhttp = function() {
  179.         if (window.XMLHttpRequest) {
  180.             var objXMLHttp = new XMLHttpRequest();
  181.  
  182.             // some older versions of Moz did not support the readyState property
  183.             // and the onreadystate event so we patch it!
  184.             if (objXMLHttp.readyState == null) {
  185.                 objXMLHttp.readyState = 0;
  186.                 objXMLHttp.addEventListener(
  187.                     "load",
  188.                     function () {
  189.                         objXMLHttp.readyState = 4;
  190.                         if (typeof(objXMLHttp.onreadystatechange) == "function") {
  191.                             objXMLHttp.onreadystatechange();
  192.                         }
  193.                     },
  194.                     false
  195.                 );
  196.             }
  197.             return objXMLHttp;
  198.         }
  199.         else {
  200.             var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
  201.             for(var n = 0; n < MSXML.length; n ++) {
  202.                 try {
  203.                     return objXMLHttp = new ActiveXObject(MSXML[n]);
  204.                 }
  205.                 catch(e) {}
  206.             }
  207.             throw new Error("Your browser does not support xmlhttp objects");
  208.         }
  209.     };
  210.     this.__args_to_array = function (args) {
  211.         argArray = [];
  212.         for (i = 0; i < args.length; i++) {
  213.             argArray[i] = args[i];
  214.         }
  215.         return argArray;
  216.     }
  217. }
  218.  
  219. phprpc_client.create = function (name, encrypt) {
  220.     eval(name + ' = new phprpc_client();');
  221.     eval(name + '.__name = "' + name + '";');
  222.     if (encrypt) {
  223.         encrypt = true;
  224.         eval(name + '.__encrypt = ' + encrypt + ';');
  225.     }
  226. }


热点文章
最新项目
相关文章 最新文章