Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davenchy/general_json_rpc
This package will help you to invoke methods across network using any protocol, This package encode and decode your requests and responses to and from bytes so you can send it any where using any protocol, This package is implementing json-rpc v2.0
https://github.com/davenchy/general_json_rpc
dart flutter json json-rpc2 rpc
Last synced: 3 months ago
JSON representation
This package will help you to invoke methods across network using any protocol, This package encode and decode your requests and responses to and from bytes so you can send it any where using any protocol, This package is implementing json-rpc v2.0
- Host: GitHub
- URL: https://github.com/davenchy/general_json_rpc
- Owner: Davenchy
- License: bsd-3-clause
- Created: 2022-04-06T02:08:33.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-29T07:48:02.000Z (almost 3 years ago)
- Last Synced: 2024-10-04T22:17:41.651Z (4 months ago)
- Topics: dart, flutter, json, json-rpc2, rpc
- Language: Dart
- Homepage:
- Size: 31.3 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# GeneralJsonRpc
GeneralJsonRpc is a package that implements json-rpc v2.0 and its purpose is to ease invoking methods and exchange data throw network
the package is focusing on encoding and decoding json-rpc messages to and from bytes
to ease sending and receiving using any protocol## A Quick Guide
In this guide you will create a server and client sides application that invokes methods throw
tcp socketslets start
- First on the server side we will define 3 methods [sum, print, quit]
- The `sum` method will get numbers list and returns their sum
- The `print` method will get a parameter named `message` and print its content on the server side
- The `quit` method will end the application by executing `exit(0);````dart
import 'dart:io';
import 'package:general_json_rpc/general_json_rpc.dart';void main() {
server();
client();
}
``````dart
void server() async {
final server = await ServerSocket.bind(InternetAddress.anyIPv4, 8081);
print('[server] Server is running on port 8081');// create MethodRunner to define the rpc methods
final runner = MethodRunner();// now lets define the 3 methods
runner.register('sum', (numbers) => numbers.reduce((a, b) => a + b));
runner.register('print', (p) => print(p['message']));runner.register('quit', (_) {
print('[server] Quit');
exit(0);
});// now lets listen for new clients
await for (final Socket client in server) {
print('[server] new client');
client.listen(
(bytes) async {
// converts bytes into [RpcObject]
final rpcObject = RpcObject.decode(bytes);// now lets handle all cases
final response = await RpcObject.auto(rpcObject, methodRunner: runner);// now lets send the response to the client if it is not null
// before send we must convert it to bytes using the `encode()` method
if (response != null) client.add(response.encode());
},
);
}
}
``````dart
void client() async {
print('[client] connecting...');
final client = await Socket.connect(InternetAddress.loopbackIPv4, 8081);
print('[client] connected!');// lets create controller to help send requests and responses
final controller = RpcController();// lets use our controller to send requests and responses
controller.sendEvent.addListener(
// ! encode [rpcMessage] before send
(rpcMessage) => client.add(rpcMessage.encode()),
);// lets listen for messages
client.listen(
(bytes) async {
// convert bytes into [RpcObject]
final rpcObject = RpcObject.decode(bytes);// now lets handle the rpcObject
RpcObject.auto(rpcObject, controller: controller);
},
);// now lets first request the `sum` method
// we will pass 3 numbers 1, 2, 3 in a list
// the sum will return as Future
// we can handle errors by listening for [RpcError]
controller.request('sum', [1, 2, 3]).then((result) {
print('[client] sum result: $result');
}).onError((error, _) {
print('[client] error: ${error.message}');
});// ============================= //
// now lets call the `print` method without expecting a response
controller.notify(
'print',
{
'message': 'A message to be printed on the server side by the client',
},
);// ============================= //
// now lets shutdown the server by calling the `quit` method after 5 seconds
// we expect no returned value
// so we will send a rpc notification
await Future.delayed(
const Duration(seconds: 5),
() => controller.notify('quit'),
);
}
```