OLD | NEW |
1 A library that implements the [JSON-RPC 2.0 spec][spec]. | 1 A library that implements the [JSON-RPC 2.0 spec][spec]. |
2 | 2 |
3 [spec]: http://www.jsonrpc.org/specification | 3 [spec]: http://www.jsonrpc.org/specification |
4 | 4 |
5 ## Server | 5 ## Server |
6 | 6 |
7 A JSON-RPC 2.0 server exposes a set of methods that can be called by clients. | 7 A JSON-RPC 2.0 server exposes a set of methods that can be called by clients. |
8 These methods can be registered using `Server.registerMethod`: | 8 These methods can be registered using `Server.registerMethod`: |
9 | 9 |
10 ```dart | 10 ```dart |
11 import "package:json_rpc_2/json_rpc_2.dart" as json_rpc; | 11 import "package:json_rpc_2/json_rpc_2.dart" as json_rpc; |
12 import "package:stream_channel/stream_channel.dart"; | 12 import "package:stream_channel/stream_channel.dart"; |
| 13 import "package:web_socket_channel/io.dart"; |
13 | 14 |
14 main() async { | 15 main() async { |
15 var socket = await WebSocket.connect('ws://localhost:4321'); | 16 var socket = IOWebSocketChannel.connect('ws://localhost:4321'); |
16 var server = new json_rpc.Server(new StreamChannel(socket, socket)); | 17 var server = new json_rpc.Server(socket); |
17 | 18 |
18 // Any string may be used as a method name. JSON-RPC 2.0 methods are | 19 // Any string may be used as a method name. JSON-RPC 2.0 methods are |
19 // case-sensitive. | 20 // case-sensitive. |
20 var i = 0; | 21 var i = 0; |
21 server.registerMethod("count", () { | 22 server.registerMethod("count", () { |
22 // Just return the value to be sent as a response to the client. This can | 23 // Just return the value to be sent as a response to the client. This can |
23 // be anything JSON-serializable, or a Future that completes to something | 24 // be anything JSON-serializable, or a Future that completes to something |
24 // JSON-serializable. | 25 // JSON-serializable. |
25 return i++; | 26 return i++; |
26 }); | 27 }); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 75 |
75 ## Client | 76 ## Client |
76 | 77 |
77 A JSON-RPC 2.0 client calls methods on a server and handles the server's | 78 A JSON-RPC 2.0 client calls methods on a server and handles the server's |
78 responses to those method calls. These methods can be called using | 79 responses to those method calls. These methods can be called using |
79 `Client.sendRequest`: | 80 `Client.sendRequest`: |
80 | 81 |
81 ```dart | 82 ```dart |
82 import "package:json_rpc_2/json_rpc_2.dart" as json_rpc; | 83 import "package:json_rpc_2/json_rpc_2.dart" as json_rpc; |
83 import "package:stream_channel/stream_channel.dart"; | 84 import "package:stream_channel/stream_channel.dart"; |
| 85 import "package:web_socket_channel/html.dart"; |
84 | 86 |
85 main() async { | 87 main() async { |
86 var socket = await WebSocket.connect('ws://localhost:4321'); | 88 var socket = HtmlWebSocketChannel.connect('ws://localhost:4321'); |
87 var client = new json_rpc.Client(new StreamChannel(socket, socket)); | 89 var client = new json_rpc.Client(socket); |
88 | 90 |
89 // This calls the "count" method on the server. A Future is returned that | 91 // This calls the "count" method on the server. A Future is returned that |
90 // will complete to the value contained in the server's response. | 92 // will complete to the value contained in the server's response. |
91 client.sendRequest("count").then((result) => print("Count is $result.")); | 93 client.sendRequest("count").then((result) => print("Count is $result.")); |
92 | 94 |
93 // Parameters are passed as a simple Map or, for positional parameters, an | 95 // Parameters are passed as a simple Map or, for positional parameters, an |
94 // Iterable. Make sure they're JSON-serializable! | 96 // Iterable. Make sure they're JSON-serializable! |
95 client.sendRequest("echo", {"message": "hello"}) | 97 client.sendRequest("echo", {"message": "hello"}) |
96 .then((echo) => print('Echo says "$echo"!')); | 98 .then((echo) => print('Echo says "$echo"!')); |
97 | 99 |
(...skipping 16 matching lines...) Expand all Loading... |
114 ``` | 116 ``` |
115 | 117 |
116 ## Peer | 118 ## Peer |
117 | 119 |
118 Although JSON-RPC 2.0 only explicitly describes clients and servers, it also | 120 Although JSON-RPC 2.0 only explicitly describes clients and servers, it also |
119 mentions that two-way communication can be supported by making each endpoint | 121 mentions that two-way communication can be supported by making each endpoint |
120 both a client and a server. This package supports this directly using the `Peer` | 122 both a client and a server. This package supports this directly using the `Peer` |
121 class, which implements both `Client` and `Server`. It supports the same methods | 123 class, which implements both `Client` and `Server`. It supports the same methods |
122 as those classes, and automatically makes sure that every message from the other | 124 as those classes, and automatically makes sure that every message from the other |
123 endpoint is routed and handled correctly. | 125 endpoint is routed and handled correctly. |
OLD | NEW |