A simple JSON based RPC protocol
main() async {
int contactIdGen = 0;
// RPC endpoint
final endpoint = new RpcEndpoint()
..route('/get/version', (_) => response(body: {'major': '1', 'minor': '0'}))
..route('/add/todo', (RpcRequest req) {
final newContact = new Contact.fromJson(req.body);
newContact.id = contactIdGen++;
contacts.contacts.add(newContact);
return response(body: contacts.json);
});
// Serve the endpoint with Jaguar http server
final server = new Jaguar();
server.addApi(rpcOnHttp(endpoint));
await server.serve();
}
Client:
main() async {
final client =
new JsonClient(new http.IOClient(), basePath: 'http://localhost:8080/');
{
final resp =
await client.post('/get/version', body: request('/get/version').toMap);
final rpcResp = new RpcResponse.decodeJson(resp.bodyStr);
print(rpcResp.status);
print(rpcResp.body);
}
{
final resp = await client.post('/add/todo',
body: request('/add/todo',
body: new Contact(name: 'teja', email: 'tejainece@gmail.com')
.json)
.toMap);
final rpcResp = new RpcResponse.decodeJson(resp.bodyStr);
print(rpcResp.status);
print(rpcResp.body);
}
}
main() async {
int contactIdGen = 0;
// RPC endpoint
final endpoint = new RpcEndpoint()
..route('/get/version', (_) => response(body: {'major': '1', 'minor': '0'}))
..route('/add/todo', (RpcRequest req) {
final newContact = new Contact.fromJson(req.body);
newContact.id = contactIdGen++;
contacts.contacts.add(newContact);
return response(body: contacts.json);
});
// Serve the endpoint with Jaguar http server
final server = new Jaguar();
server.get('/ws', rpcOnWebSocket(endpoint));
await server.serve();
}
Client:
main() async {
// Client
final WebSocket socket = await WebSocket.connect('ws://localhost:8080/ws');
final Stream<RpcResponse> data =
socket.asBroadcastStream().map((d) => new RpcResponse.decodeJson(d));
{
socket.add(request('/get/version').json);
final RpcResponse rpcResp = await data.first;
print(rpcResp.status);
print(rpcResp.body);
}
{
socket.add(request('/add/todo',
body: new Contact(name: 'teja', email: 'tejainece@gmail.com').toMap)
.json);
final RpcResponse rpcResp = await data.first;
print(rpcResp.status);
print(rpcResp.body);
}
}
main() async {
// Client
final RpcWebSocketClient socket =
await RpcWebSocketClient.connect('ws://localhost:8080/ws');
{
final RpcResponse rpcResp = await socket.send(request('/get/version'));
print(rpcResp.status);
print(rpcResp.body);
}
{
final RpcResponse rpcResp = await socket.send(request('/add/todo',
body: new Contact(name: 'teja', email: 'tejainece@gmail.com').toMap));
print(rpcResp.status);
print(rpcResp.body);
}
}
TODO
TODO
main() {
int contactIdGen = 0;
final server = new RpcEndpoint()
..route('/get/version', (_) => response(body: {'major': '1', 'minor': '0'}))
..route('/add/todo', (RpcRequest req) {
final newContact = new Contact.fromJson(req.body);
newContact.id = contactIdGen++;
contacts.contacts.add(newContact);
return response(body: contacts.json);
});
{
final RpcResponse resp = server.handleRequest(request('/get/unknown'));
print(resp.status);
}
{
final RpcResponse resp = server.handleRequest(request('/add/todo',
body: new Contact(name: 'teja', email: 'tejainece@gmail.com').json));
print(resp.status);
print(resp.body);
}
}
TODO
Add this to your package's pubspec.yaml file:
dependencies:
jaguar_rpc: ^1.3.1
You can install packages from the command line:
with pub:
$ pub get
Alternatively, your editor might support pub get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:jaguar_rpc/jaguar_rpc.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
1.3.1 | Feb 2, 2018 |
|
|
1.1.5 | Jan 11, 2018 |
|
|
1.0.0 | Dec 23, 2017 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
0
|
Health:
Code health derived from static analysis.
[more]
|
--
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
--
|
Overall:
Weighted score of the above.
[more]
|
0
|
The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.
Support Dart 2 in pubspec.yaml
.
The SDK constraint in pubspec.yaml
doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.
Make sure dartdoc
successfully runs on your package's source files. (-10 points)
Dependencies were not resolved.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.20.1 <2.0.0 |