֪ʶѧϰ֮Json-RPC JSON-RPC¼¼Êõ
JSON-RPCÊÇÒ»¸öÎÞ״̬ÇÒÇáÁ¿¼¶µÄÔ¶³Ì¹ý³Ìµ÷ÓÃ(RPC)ÐÒé¡£ ±¾¹æ·¶Ö÷Òª¶¨ÒåÁËһЩÊý¾Ý½á¹¹¼°ÆäÏà¹ØµÄ´¦Àí¹æÔò¡£ËüÔÊÐíÔËÐÐÔÚ»ùÓÚsocket,httpµÈÖî¶à²»Í¬ÏûÏ¢´«Êä»·¾³µÄͬһ½ø³ÌÖС£
ÐÒéÃèÊö data sent to Server{ "method": "sayHello", "params": ["Hello JSON-RPC"], "id": 1}
²ÎÊý½éÉÜ:<br/>
method£º µ÷Óõķ½·¨Ãû
params£º ·½·¨´«ÈëµÄ²ÎÊý£¬ÈôÎÞ²ÎÊýÔò´«Èë []
id £º µ÷Óñêʶ·û£¬ÓÃÓÚ±êʾһ´ÎÔ¶³Ìµ÷Óùý³Ì£»Òѽ¨Á¢¿Í»§¶ËµÄΨһ±êʶid£¬Öµ±ØÐë°üº¬Ò»¸ö×Ö·û´®¡¢ÊýÖµ»òNULL¿ÕÖµ¡£Èç¹û²»°üº¬¸Ã³ÉÔ±Ôò±»È϶¨ÎªÊÇÒ»¸ö֪ͨ¡£
Note
·þÎñ¶Ë±ØÐë»Ø´ðÏàͬµÄÖµÈç¹û°üº¬ÔÚÏìÓ¦¶ÔÏó¡£ Õâ¸ö³ÉÔ±ÓÃÀ´Á½¸ö¶ÔÏóÖ®¼äµÄ¹ØÁªÉÏÏÂÎÄ¡£
[1] ÔÚÇëÇó¶ÔÏóÖв»½¨ÒéʹÓÃNULL×÷ΪidÖµ£¬ÒòΪ¸Ã¹æ·¶½«Ê¹ÓÿÕÖµÈ϶¨ÎªÎ´ÖªidµÄÇëÇó¡£ÁíÍ⣬ÓÉÓÚJSON-RPC 1.0 µÄ֪ͨʹÓÃÁË¿ÕÖµ£¬Õâ¿ÉÄÜÒýÆð´¦ÀíÉϵĻìÏý¡£
[2] ʹÓÃСÊýÊDz»È·¶¨ÐԵģ¬ÒòΪÐí¶àÊ®½øÖÆСÊý²»Äܾ«×¼µÄ±í´ïΪ¶þ½øÖÆСÊý¡£
data sent to Client{ "result":"Hello JSON-RPC","error",null,"id":1}
²ÎÊý½éÉÜ:<br/>
result: ·½·¨·µ»ØÖµ£¬ÈôÎÞ·µ»ØÖµ£¬Ôò·µ»Ønull¡£Èôµ÷ÓôíÎ󣬷µ»Ønull¡£
error £ºµ÷ÓÃʱ´íÎó£¬ÎÞ´íÎó·µ»Ønull¡£
¸Ã³ÉÔ±ÔÚʧ°ÜÊDZØÐë°üº¬¡£
µ±Ã»ÓÐÒýÆð´íÎóµÄʱ±ØÐë²»°üº¬¸Ã³ÉÔ±¡£
id: µ÷Óñêʶ·û£¬Óëµ÷Ó÷½´«ÈëµÄ±êʶ·ûÒ»Ö¡£
¸Ã³ÉÔ±Öµ±ØÐëÓÚÇëÇó¶ÔÏóÖеÄid³ÉÔ±ÖµÒ»Ö¡£
ÈôÔÚ¼ì²éÇëÇó¶ÔÏóidʱ´íÎó£¨ÀýÈç²ÎÊý´íÎó»òÎÞЧÇëÇ󣩣¬Ôò¸ÃÖµ±ØÐëΪ¿ÕÖµ¡£
ûÓаüº¬¡°id¡±³ÉÔ±µÄÇëÇó¶ÔÏóΪ֪ͨ
exampleËùÓж¼ÎªÍ¨ÖªµÄrpcÅúÁ¿µ÷ÓÃ:
--> [
{"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}
]
<-- //Nothing is returned for all notification batches
rpcµ÷ÓÃÈç¹û´æÔÚ²ÎÊýÔò±ØÐëΪ»ù±¾ÀàÐÍ»ò½á¹¹»¯ÀàÐ͵IJÎÊýÖµ£¬ÒªÃ´ÎªË÷ÒýÊý×飬ҪôΪ¹ØÁªÊý×é¶ÔÏó¡£
Ë÷Òý£º²ÎÊý±ØÐëΪÊý×飬²¢°üº¬Óë·þÎñ¶ËÔ¤ÆÚ˳ÐòÒ»ÖµIJÎÊýÖµ¡£
¹ØÁªÃû³Æ£º²ÎÊý±ØÐëΪ¶ÔÏ󣬲¢°üº¬Óë·þÎñ¶ËÏàÆ¥ÅäµÄ²ÎÊý³ÉÔ±Ãû³Æ¡£Ã»ÓÐÔÚÔ¤ÆÚÖеijÉÔ±Ãû³Æ¿ÉÄÜ»áÒýÆð´íÎó¡£Ãû³Æ±ØÐëÍêÈ«Æ¥Å䣬°üÀ¨·½·¨µÄÔ¤ÆÚ²ÎÊýÃûÒÔ¼°´óСд¡£
error object{"code":"", message:"","data":""}
code:ʹÓÃÊýÖµ±íʾ¸ÃÒì³£µÄ´íÎóÀàÐÍ¡£ ±ØÐëΪÕûÊý¡£
message:¶Ô¸Ã´íÎóµÄ¼òµ¥ÃèÊö×Ö·û´®¡£ ¸ÃÃèÊöÓ¦¾¡Á¿ÏÞ¶¨ÔÚ¼ò¶ÌµÄÒ»¾ä»°¡£
data:°üº¬¹ØÓÚ´íÎ󸽼ÓÐÅÏ¢µÄ»ù±¾ÀàÐÍ»ò½á¹¹»¯ÀàÐÍ¡£¸Ã³ÉÔ±¿ÉºöÂÔ¡£ ¸Ã³ÉÔ±ÖµÓÉ·þÎñ¶Ë¶¨Ò壨ÀýÈçÏêϸµÄ´íÎóÐÅÏ¢£¬Ç¶Ì׵ĴíÎóµÈ£©¡£
json-rpcÊÇÒ»Öַdz£ÇáÁ¿¼¶µÄ¿çÓïÑÔÔ¶³Ìµ÷ÓÃÐÒ飬ʵÏÖ¼°Ê¹Óüòµ¥¡£½öÐ輸ʮÐдúÂ룬¼´¿ÉʵÏÖÒ»¸öÔ¶³Ìµ÷ÓõĿͻ§¶Ë£¬·½±ãÓïÑÔÀ©Õ¹¿Í»§¶ËµÄʵÏÖ¡£
ÖÁÓÚ WEB¿ª·¢ÖУ¬Ê¹ÓÃJSON-RPCºÃ£¬»¹ÊÇRESTful APIºÃ£¿
¿ÉÒÔÒƲ½ÕâÀï
1.
¡¡