Skip to content

Commit 70d5d3c

Browse files
committed
Add forward plugin
1 parent 0fcef59 commit 70d5d3c

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

lib/plugins.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| |
99
| hprose.rpc.plugins library for Dart. |
1010
| |
11-
| LastModified: Mar 6, 2019 |
11+
| LastModified: Mar 28, 2020 |
1212
| Author: Ma Bingyao <[email protected]> |
1313
| |
1414
\*________________________________________________________*/
@@ -22,7 +22,11 @@ import 'dart:math';
2222
import 'dart:convert';
2323
import 'dart:typed_data';
2424
import 'io.dart';
25-
import 'core.dart';
25+
import 'core.dart'
26+
// ignore: uri_does_not_exist
27+
if (dart.library.html) 'browser.dart'
28+
// ignore: uri_does_not_exist
29+
if (dart.library.io) 'rpc.dart';
2630

2731
part 'src/rpc/plugins/log.dart';
2832
part 'src/rpc/plugins/limiter.dart';
@@ -33,3 +37,4 @@ part 'src/rpc/plugins/loadbalance.dart';
3337
part 'src/rpc/plugins/push.dart';
3438
part 'src/rpc/plugins/reverse.dart';
3539
part 'src/rpc/plugins/timeout.dart';
40+
part 'src/rpc/plugins/forward.dart';

lib/src/rpc/plugins/forward.dart

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*--------------------------------------------------------*\
2+
| |
3+
| hprose |
4+
| |
5+
| Official WebSite: https://hprose.com |
6+
| |
7+
| forward.dart |
8+
| |
9+
| Forward plugin for Dart. |
10+
| |
11+
| LastModified: Mar 28, 2020 |
12+
| Author: Ma Bingyao <[email protected]> |
13+
| |
14+
\*________________________________________________________*/
15+
16+
part of hprose.rpc.plugins;
17+
18+
class Forward {
19+
Client _client;
20+
Duration timeout;
21+
Forward([List<String> uris]) {
22+
_client = new Client(uris);
23+
}
24+
25+
Future<Uint8List> ioHandler(
26+
Uint8List request, Context context, NextIOHandler next) {
27+
final clientContext = new ClientContext(timeout: timeout);
28+
clientContext.init(_client);
29+
return _client.request(request, clientContext);
30+
}
31+
32+
Future invokeHandler(
33+
String name, List args, Context context, NextInvokeHandler next) {
34+
final clientContext = new ClientContext(timeout: timeout);
35+
return _client.invoke(name, args, clientContext);
36+
}
37+
38+
void use<Handler>(Handler handler) {
39+
_client.use(handler);
40+
}
41+
42+
void unuse<Handler>(Handler handler) {
43+
_client.unuse(handler);
44+
}
45+
}

test/hprose_rpc_test.dart

+18
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,22 @@ void main() {
503503
await provider.close();
504504
server.close();
505505
});
506+
507+
test('forward', () async {
508+
final service = Service();
509+
service.addMethod(hello);
510+
final server = await ServerSocket.bind('127.0.0.1', 8412);
511+
service.bind(server);
512+
513+
final service2 = Service();
514+
service2.use(new Forward(["tcp://127.0.0.1:8412"]).ioHandler);
515+
final server2 = await HttpServer.bind('127.0.0.1', 8000);
516+
service2.bind(server2);
517+
518+
final client = Client(['http://127.0.0.1:8000/']);
519+
final proxy = client.useService();
520+
expect(await proxy.hello<String>('world'), equals('hello world'));
521+
server2.close();
522+
server.close();
523+
});
506524
}

0 commit comments

Comments
 (0)