Index: README.md |
diff --git a/README.md b/README.md |
index 67afc31215f163569d55db9d2aa154fcf9451da5..8078fa273fd140b34eee1ac98a14fb246baa5e10 100644 |
--- a/README.md |
+++ b/README.md |
@@ -9,68 +9,66 @@ These methods can be registered using `Server.registerMethod`: |
```dart |
import "package:json_rpc_2/json_rpc_2.dart" as json_rpc; |
+import "package:stream_channel/stream_channel.dart"; |
+ |
+main() async { |
+ var socket = await WebSocket.connect('ws://localhost:4321'); |
+ var server = new json_rpc.Server(new StreamChannel(socket, socket)); |
+ |
+ // Any string may be used as a method name. JSON-RPC 2.0 methods are |
+ // case-sensitive. |
+ var i = 0; |
+ server.registerMethod("count", () { |
+ // Just return the value to be sent as a response to the client. This can |
+ // be anything JSON-serializable, or a Future that completes to something |
+ // JSON-serializable. |
+ return i++; |
+ }); |
+ |
+ // Methods can take parameters. They're presented as a [Parameters] object |
+ // which makes it easy to validate that the expected parameters exist. |
+ server.registerMethod("echo", (params) { |
+ // If the request doesn't have a "message" parameter, this will |
+ // automatically send a response notifying the client that the request |
+ // was invalid. |
+ return params.getNamed("message"); |
+ }); |
+ |
+ // [Parameters] has methods for verifying argument types. |
+ server.registerMethod("subtract", (params) { |
+ // If "minuend" or "subtrahend" aren't numbers, this will reject the |
+ // request. |
+ return params.getNum("minuend") - params.getNum("subtrahend"); |
+ }); |
-void main() { |
- WebSocket.connect('ws://localhost:4321').then((socket) { |
- // You can start the server with a Stream for requests and a StreamSink for |
- // responses, or with an object that's both, like a WebSocket. |
- var server = new json_rpc.Server(socket); |
- |
- // Any string may be used as a method name. JSON-RPC 2.0 methods are |
- // case-sensitive. |
- var i = 0; |
- server.registerMethod("count", () { |
- // Just return the value to be sent as a response to the client. This can |
- // be anything JSON-serializable, or a Future that completes to something |
- // JSON-serializable. |
- return i++; |
- }); |
- |
- // Methods can take parameters. They're presented as a [Parameters] object |
- // which makes it easy to validate that the expected parameters exist. |
- server.registerMethod("echo", (params) { |
- // If the request doesn't have a "message" parameter, this will |
- // automatically send a response notifying the client that the request |
- // was invalid. |
- return params.getNamed("message"); |
- }); |
- |
- // [Parameters] has methods for verifying argument types. |
- server.registerMethod("subtract", (params) { |
- // If "minuend" or "subtrahend" aren't numbers, this will reject the |
- // request. |
- return params.getNum("minuend") - params.getNum("subtrahend"); |
- }); |
- |
- // [Parameters] also supports optional arguments. |
- server.registerMethod("sort", (params) { |
- var list = params.getList("list"); |
- list.sort(); |
- if (params.getBool("descending", orElse: () => false)) { |
- return params.list.reversed; |
- } else { |
- return params.list; |
- } |
- }); |
- |
- // A method can send an error response by throwing a |
- // `json_rpc.RpcException`. Any positive number may be used as an |
- // application- defined error code. |
- const DIVIDE_BY_ZERO = 1; |
- server.registerMethod("divide", (params) { |
- var divisor = params.getNum("divisor"); |
- if (divisor == 0) { |
- throw new json_rpc.RpcException( |
- DIVIDE_BY_ZERO, "Cannot divide by zero."); |
- } |
- |
- return params.getNum("dividend") / divisor; |
- }); |
- |
- // To give you time to register all your methods, the server won't actually |
- // start listening for requests until you call `listen`. |
- server.listen(); |
+ // [Parameters] also supports optional arguments. |
+ server.registerMethod("sort", (params) { |
+ var list = params.getList("list"); |
+ list.sort(); |
+ if (params.getBool("descending", orElse: () => false)) { |
+ return params.list.reversed; |
+ } else { |
+ return params.list; |
+ } |
}); |
+ |
+ // A method can send an error response by throwing a |
+ // `json_rpc.RpcException`. Any positive number may be used as an |
+ // application- defined error code. |
+ const DIVIDE_BY_ZERO = 1; |
+ server.registerMethod("divide", (params) { |
+ var divisor = params.getNum("divisor"); |
+ if (divisor == 0) { |
+ throw new json_rpc.RpcException( |
+ DIVIDE_BY_ZERO, "Cannot divide by zero."); |
+ } |
+ |
+ return params.getNum("dividend") / divisor; |
+ }); |
+ |
+ // To give you time to register all your methods, the server won't actually |
+ // start listening for requests until you call `listen`. |
+ server.listen(); |
} |
``` |
@@ -82,38 +80,36 @@ responses to those method calls. These methods can be called using |
```dart |
import "package:json_rpc_2/json_rpc_2.dart" as json_rpc; |
- |
-void main() { |
- WebSocket.connect('ws://localhost:4321').then((socket) { |
- // Just like the server, a client takes a Stream and a StreamSink or a |
- // single object that's both. |
- var client = new json_rpc.Client(socket); |
- |
- // This calls the "count" method on the server. A Future is returned that |
- // will complete to the value contained in the server's response. |
- client.sendRequest("count").then((result) => print("Count is $result.")); |
- |
- // Parameters are passed as a simple Map or, for positional parameters, an |
- // Iterable. Make sure they're JSON-serializable! |
- client.sendRequest("echo", {"message": "hello"}) |
- .then((echo) => print('Echo says "$echo"!')); |
- |
- // A notification is a way to call a method that tells the server that no |
- // result is expected. Its return type is `void`; even if it causes an |
- // error, you won't hear back. |
- client.sendNotification("count"); |
- |
- // If the server sends an error response, the returned Future will complete |
- // with an RpcException. You can catch this error and inspect its error |
- // code, message, and any data that the server sent along with it. |
- client.sendRequest("divide", {"dividend": 2, "divisor": 0}) |
- .catchError((error) { |
- print("RPC error ${error.code}: ${error.message}"); |
- }); |
- |
- // The client won't subscribe to the input stream until you call `listen`. |
- client.listen(); |
+import "package:stream_channel/stream_channel.dart"; |
+ |
+main() async { |
+ var socket = await WebSocket.connect('ws://localhost:4321'); |
+ var client = new json_rpc.Client(new StreamChannel(socket, socket)); |
+ |
+ // This calls the "count" method on the server. A Future is returned that |
+ // will complete to the value contained in the server's response. |
+ client.sendRequest("count").then((result) => print("Count is $result.")); |
+ |
+ // Parameters are passed as a simple Map or, for positional parameters, an |
+ // Iterable. Make sure they're JSON-serializable! |
+ client.sendRequest("echo", {"message": "hello"}) |
+ .then((echo) => print('Echo says "$echo"!')); |
+ |
+ // A notification is a way to call a method that tells the server that no |
+ // result is expected. Its return type is `void`; even if it causes an |
+ // error, you won't hear back. |
+ client.sendNotification("count"); |
+ |
+ // If the server sends an error response, the returned Future will complete |
+ // with an RpcException. You can catch this error and inspect its error |
+ // code, message, and any data that the server sent along with it. |
+ client.sendRequest("divide", {"dividend": 2, "divisor": 0}) |
+ .catchError((error) { |
+ print("RPC error ${error.code}: ${error.message}"); |
}); |
+ |
+ // The client won't subscribe to the input stream until you call `listen`. |
+ client.listen(); |
} |
``` |