PHPRPC 2.1 Clients implemented in Ajax mode

Source: Internet
Author: User
Ajax| Client

This article gives the Ajax client implementation of the PHPRPC 2.1 protocol . The client is used in much the same way as version 2.0. The difference is that if the remote function you want to invoke is a reference parameter pass, you need to set the function's Ref property to True before calling it. This property indicates whether the reference parameter is passed. This property defaults to False. It is recommended that you set the Onready event in the client object.

The following is the implementation code for the client:

Download: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 are free. You can redistribute it and/or modify it.
  7. * http://www.coolcode.cn/?p=146
  8. */
  9. /*
  10. * Interfaces:
  11. * Phprpc_client.create (' RPC ');
  12. * Rpc.use_service (' http://domain.com/phprpc/server.php ');
  13. * Rpc.method_callback = function (result, args, output) {
  14. * If (result instanceof Phprpc_error) {
  15. * Alert (RESULT.ERRSTR);
  16. *     }
  17. * Else {
  18. * Alert (result); Or do no other things.
  19. *     }
  20. * }
  21. * ....
  22. * IF (Rpc.ready) Rpc.method ();
  23. */
  24. function Phprpc_error (errno, errstr) {
  25. This.errno = errno;
  26. This.errstr = Errstr;
  27. }
  28. function Phprpc_client () {
  29. this.__php = new Php_serializer ();
  30. This.__url = ';
  31. This.__encrypt = false;
  32. This.encrypt = 0;
  33. This.async = true;
  34. This.ready = false;
  35. This.args = null;
  36. This.output = "";
  37. This.use_service = function (URL, encrypt) {
  38. if (typeof (encrypt) = = "undefined") {
  39. encrypt = This.__encrypt;
  40. }
  41. if (typeof (this.__name) = = "undefined") {
  42. return false;
  43. }
  44. This.__url = URL;
  45. var xmlhttp = this.__create_xmlhttp ();
  46. var __rpc = this;
  47. if (encrypt = = True) {
  48. Xmlhttp.onreadystatechange = function () {
  49. if (xmlhttp.readystate = = 4) {
  50. if (Xmlhttp.responsetext) {
  51. eval (xmlhttp.responsetext);
  52. if (typeof (phprpc_encrypt) = = "undefined") {
  53. __rpc.__encrypt = false;
  54. __rpc.use_service (__rpc.__url);
  55. }
  56. else {
  57. __rpc.__encrypt = __rpc.__php.unserialize (Phprpc_encrypt);
  58. __rpc.__encrypt[' P '] = dec2num (__rpc.__encrypt[' P '));
  59. __rpc.__encrypt[' g '] = Dec2num (__rpc.__encrypt[' G '));
  60. __rpc.__encrypt[' y '] = dec2num (__rpc.__encrypt[' y '));
  61. __rpc.__encrypt[' x '] = rand (127, 1);
  62. var key = Pow_mod (__rpc.__encrypt[' y '),
  63. __rpc.__encrypt[' x '],
  64. __rpc.__encrypt[' P ']);
  65. Key = Num2str (key);
  66. for (var i = 0; i < 16-key.length; i++) {
  67. Key = ' + ' + key;
  68. }
  69. __rpc.__encrypt[' k '] = key;
  70. var encrypt = Pow_mod (__rpc.__encrypt[' G '),
  71. __rpc.__encrypt[' x '],
  72. __rpc.__encrypt[' P ']);
  73. __rpc.use_service (__rpc.__url, Num2dec (encrypt). Replace (/\+/g, '%2b '));
  74. }
  75. }
  76. Delete (XMLHTTP);
  77. }
  78. }
  79. }
  80. else {
  81. Xmlhttp.onreadystatechange = function () {
  82. if (xmlhttp.readystate = = 4) {
  83. if (Xmlhttp.responsetext) {
  84. eval (xmlhttp.responsetext);
  85. var functions = __rpc.__php.unserialize (phprpc_functions);
  86. var func;
  87. for (var i = 0; i < functions.length; i++) {
  88. Func = __rpc.__name + "." + functions[i] + "= function () {\ r \ n";
  89. Func + = "This.__call (' + functions[i] +" ', This.__args_to_array (arguments)); \ r \ n ";
  90. Func + = "}\r\n";
  91. Func + = __rpc.__name + "." + Functions[i] + ". ref = false;\r\n";
  92. Eval (func);
  93. }
  94. __rpc.ready = true;
  95. if (typeof (__rpc.onready) = = "function") {
  96. __rpc.onready ();
  97. }
  98. }
  99. Delete (XMLHTTP);
  100. }
  101. }
  102. }
  103. Xmlhttp.open ("Get", This.__url + '? phprpc_encrypt= ' + Encrypt + ' &phprpc_encode=false ', true);
  104. Xmlhttp.send (NULL);
  105. };
  106. This.__call = function (func, args) {
  107. var __args = this.__php.serialize (args);
  108. if ((This.__encrypt!== false) && (This.encrypt > 0)) {
  109. __args = Xxtea_encrypt (__args, this.__encrypt[' K '));
  110. }
  111. __args = Base64Encode (__args);
  112. var request = ' phprpc_func= ' + func
  113. + ' &phprpc_args= ' + __args
  114. + ' &phprpc_encode=false '
  115. + ' &phprpc_encrypt= ' + this.encrypt;
  116. var ref = eval (This.__name + "." + func + ". Ref");
  117. if (!ref) {
  118. Request + = ' &phprpc_ref=false ';
  119. }
  120. var xmlhttp = this.__create_xmlhttp ();
  121. var session = {' args ': args, ' ref ': REF, ' Encrypt ': This.encrypt};
  122. if (This.async) {
  123. var __rpc = this;
  124. Xmlhttp.onreadystatechange = function () {
  125. if (xmlhttp.readystate = = 4) {
  126. if (Xmlhttp.responsetext) {
  127. __rpc.__get_result (XMLHTTP, session);
  128. if (typeof (__rpc.__name +. + func + "_callback")) = = "function") {
  129. Eval (__rpc.__name + "." + func + "_callback (Phprpc_result, Phprpc_args, phprpc_output);");
  130. }
  131. }
  132. Delete (XMLHTTP);
  133. }
  134. }
  135. }
  136. Xmlhttp.open ("Post", This.__url, This.async);
  137. Xmlhttp.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded; Charset=utf-8 ');
  138. Xmlhttp.send (Request.replace (/\+/g, '%2b '));
  139. if (!this.async) {
  140. if (Xmlhttp.responsetext) {
  141. This.__get_result (XMLHTTP, session);
  142. This.output = Phprpc_output;
  143. This.args = Phprpc_args;
  144. return phprpc_result;
  145. }
  146. else {
  147. Return to New Phprpc_error (1, "No data received from server");
  148. }
  149. Delete (XMLHTTP);
  150. }
  151. };
  152. This.__get_result = function (XMLHTTP, session) {
  153. eval (xmlhttp.responsetext);
  154. if (Phprpc_errno = = 0) {
  155. if ((This.__encrypt!== false) && (Session.encrypt > 0)) {
  156. if (Session.encrypt > 1) {
  157. Phprpc_result = Xxtea_decrypt (Phprpc_result, this.__encrypt[' K '));
  158. }
  159. if (session.ref) {
  160. Phprpc_args = Xxtea_decrypt (Phprpc_args, this.__encrypt[' K '));
  161. }
  162. }
  163. Phprpc_result = This.__php.unserialize (Phprpc_result);
  164. if (session.ref) {
  165. Phprpc_args = This.__php.unserialize (Phprpc_args);
  166. }
  167. else {
  168. Phprpc_args = This.args;
  169. }
  170. }
  171. else {
  172. Phprpc_result = new Phprpc_error (Phprpc_errno, PHPRPC_ERRSTR);
  173. Phprpc_args = null;
  174. }
  175. }
  176. The function __create_xmlhttp modified from
  177. Http://webfx.eae.net/dhtml/xmlextras/xmlextras.html and
  178. http://www.ugia.cn/?p=85
  179. This.__create_xmlhttp = function () {
  180. if (window. XMLHttpRequest) {
  181. var objxmlhttp = new XMLHttpRequest ();
  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");
  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. Phprpc_client.create = function (name, encrypt) {
  219. Eval (name + ' = new phprpc_client (); ');
  220. Eval (name + '. __name = ' + name + '; ');
  221. if (encrypt) {
  222. Encrypt = true;
  223. Eval (name + '. __encrypt = ' + encrypt + '; ');
  224. }
  225. }

Author Andot

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.