commit | 52e957d3d62e456484af36615243f6a738e89fcb | [log] [tgz] |
---|---|---|
author | nweiz@google.com <nweiz@google.com> | Wed Jun 04 20:40:45 2014 +0000 |
committer | nweiz@google.com <nweiz@google.com> | Wed Jun 04 20:40:45 2014 +0000 |
tree | e09564d95e4ec0cba061c8426d1d251a245eef82 | |
parent | 0ad8bb9c7473a2f782206fd9d186ce77a8bc073d [diff] |
Convert json_rpc.Server to take a Stream and StreamSink. R=rnystrom@google.com Review URL: https://codereview.chromium.org//309503005 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/json_rpc_2@37012 260f80e4-7a28-3924-810f-c04153c831b5
A library that implements the JSON-RPC 2.0 spec.
A JSON-RPC 2.0 server exposes a set of methods that can be called by clients. These methods can be registered using Server.registerMethod
:
import "package:json_rpc_2/json_rpc_2.dart" as json_rpc; var server = new json_rpc.Server(); // 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; });
Once you've registered your methods, you can handle requests with Server.parseRequest
:
import 'dart:io'; WebSocket.connect('ws://localhost:4321').then((socket) { socket.listen((message) { server.parseRequest(message).then((response) { if (response != null) socket.add(response); }); }); });
If you‘re communicating with objects that haven’t been serialized to a string, you can also call Server.handleRequest
directly:
import 'dart:isolate'; var receive = new ReceivePort(); Isolate.spawnUri('path/to/client.dart', [], receive.sendPort).then((_) { receive.listen((message) { server.handleRequest(message['request']).then((response) { if (response != null) message['respond'].send(response); }); }); })
Currently this package does not contain an implementation of a JSON-RPC 2.0 client.