rpc.txt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. library fis_jsonrpc;
  2. import 'dart:collection';
  3. import 'package:fis_common/extensions/type.dart';
  4. import 'client_base.dart';
  5. import 'interceptor.dart';
  6. import 'services/index.dart';
  7. export 'services/index.dart';
  8. export 'request.dart';
  9. export 'exception.dart';
  10. export 'interceptor.dart';
  11. export 'notifications/index.dart';
  12. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  13. /// 默认地址标识
  14. const String _defaultAddressSign = "FLYINSONO";
  15. /// JSON-RPC 代理
  16. class JsonRpcProxy {
  17. JsonRpcProxy({
  18. this.defaultServerHost = "cloud.fis.plus",
  19. this.platformHost = "platform.fis.plus",
  20. }) {
  21. _currentHost = this.defaultServerHost;
  22. }
  23. /// 默认主机地址
  24. final String defaultServerHost;
  25. /// 宿主平台代理地址
  26. final String platformHost;
  27. late String _currentHost;
  28. /// 当前服务主机地址
  29. String get currentHostAddress => "http://$_currentHost";
  30. String get defaultAddressSign => _defaultAddressSign;
  31. HashMap<Type, dynamic> _serviceCache = HashMap();
  32. static PlatformService? _platformService;
  33. /* 服务代理设置 Start */
  34. /// 平台服务
  35. PlatformService get platform {
  36. if (_platformService == null)
  37. _platformService = PlatformService("http://$platformHost");
  38. return _platformService!;
  39. }
  40. *******Separator******
  41. /* 服务代理设置 End */
  42. /// 设置服务主机地址
  43. void setServerHost(String address) {
  44. if (address == _defaultAddressSign) {
  45. address = defaultServerHost;
  46. }
  47. _currentHost = address;
  48. }
  49. /// 添加拦截器
  50. void addInterceptor(JsonRpcInterceptor interceptor) =>
  51. jsonRpcInterceptHost.addInterceptor(interceptor);
  52. /// 清空缓存
  53. void clearCache() => _serviceCache.clear();
  54. /// 查找Service实例
  55. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  56. Type serviceType = typeOf<T>();
  57. if (!_serviceCache.containsKey(serviceType)) {
  58. _serviceCache[serviceType] = builder.call();
  59. }
  60. return _serviceCache[serviceType] as T;
  61. }
  62. }