From d7e8649c1781c7a6bd9d0da58084f1ed410b2e0e Mon Sep 17 00:00:00 2001 From: yangjiejie12308 <38904385+yangjiejie12308@users.noreply.github.com> Date: Mon, 10 Jul 2023 10:46:43 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E9=87=8D=E5=A4=8D=E6=94=B6=E5=88=B0?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.flutter-plugins-dependencies | 2 +- cim-client-sdk/cim-flutter-sdk/CHANGELOG.md | 4 +- cim-client-sdk/cim-flutter-sdk/README.md | 42 ++++++++++----- .../cim-flutter-sdk/example/lib/main.dart | 47 ++++++++-------- .../cim-flutter-sdk/example/pubspec.lock | 2 +- .../cim-flutter-sdk/lib/cim_flutter_sdk.dart | 21 -------- .../lib/cim_flutter_sdk_method_channel.dart | 17 ------ .../cim_flutter_sdk_platform_interface.dart | 29 ---------- .../cim-flutter-sdk/lib/cim_socket.dart | 54 +++++-------------- cim-client-sdk/cim-flutter-sdk/pubspec.yaml | 3 +- 10 files changed, 67 insertions(+), 154 deletions(-) delete mode 100644 cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk.dart delete mode 100644 cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_method_channel.dart delete mode 100644 cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_platform_interface.dart diff --git a/cim-client-sdk/cim-flutter-sdk/.flutter-plugins-dependencies b/cim-client-sdk/cim-flutter-sdk/.flutter-plugins-dependencies index 362bbf9..7529ebd 100644 --- a/cim-client-sdk/cim-flutter-sdk/.flutter-plugins-dependencies +++ b/cim-client-sdk/cim-flutter-sdk/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":true,"dependencies":[]}],"linux":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":false,"dependencies":[]}],"windows":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":false,"dependencies":[]}],"web":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"device_info_plus","dependencies":[]}],"date_created":"2023-07-06 11:06:37.470895","version":"3.7.7"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":true,"dependencies":[]}],"linux":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":false,"dependencies":[]}],"windows":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","native_build":false,"dependencies":[]}],"web":[{"name":"device_info_plus","path":"C:\\\\Users\\\\smeb\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\device_info_plus-8.2.2\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"device_info_plus","dependencies":[]}],"date_created":"2023-07-07 10:37:13.109602","version":"3.7.7"} \ No newline at end of file diff --git a/cim-client-sdk/cim-flutter-sdk/CHANGELOG.md b/cim-client-sdk/cim-flutter-sdk/CHANGELOG.md index 0aae02e..bc44140 100644 --- a/cim-client-sdk/cim-flutter-sdk/CHANGELOG.md +++ b/cim-client-sdk/cim-flutter-sdk/CHANGELOG.md @@ -1,3 +1,3 @@ -## 1.0.2 +## 1.0.3 -* 修复version字段过长的错误 +* 修复重复收到消息的BUG diff --git a/cim-client-sdk/cim-flutter-sdk/README.md b/cim-client-sdk/cim-flutter-sdk/README.md index 76e9e86..1fe0048 100644 --- a/cim-client-sdk/cim-flutter-sdk/README.md +++ b/cim-client-sdk/cim-flutter-sdk/README.md @@ -9,7 +9,7 @@ ``` dependencies: - cim_flutter_sdk: ^1.0.2 + cim_flutter_sdk: ^1.0.3 ``` @@ -19,30 +19,44 @@ dependencies: import 'package:cim_flutter_sdk/cim_socket.dart'; ... -final CIMSocket cimSocket = CIMSocket(); + late CIMSocket? cimSocket = null; -String Message = 'Unknown'; + late List list = []; - cimSocket.init('127.0.0.1', 34567, 16501516154949); - cimSocket.connect(); - cimSocket.addListener(() { - if (cimSocket.model == null) return; - setState(() { - Message = cimSocket.model!.toProto3Json().toString(); - }); + late bool connectStatus = false; + + @override + void initState() { + super.initState(); + cimSocket = CIMSocket(onMessageReceived: (value) { + setState(() { + list.add(value.toProto3Json().toString()); }); - + }, onConnectionStatusChanged: (value) { + setState(() { + connectStatus = value; + }); + }); + cimSocket!.init('127.0.0.1', 34567, 16501516154949); + cimSocket!.connect(); + } ... + @override + Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( - title: const Text('Plugin example app'), + title: Text(connectStatus ? '已连接' : '未连接'), ), - body: Center( - child: Text('Message is: $Message\n'), + body: ListView.builder( + itemCount: list.length, + itemBuilder: (context, index) { + return Text(list[index]); + }, ), ), ); + } ``` diff --git a/cim-client-sdk/cim-flutter-sdk/example/lib/main.dart b/cim-client-sdk/cim-flutter-sdk/example/lib/main.dart index cd8bb01..6562fee 100644 --- a/cim-client-sdk/cim-flutter-sdk/example/lib/main.dart +++ b/cim-client-sdk/cim-flutter-sdk/example/lib/main.dart @@ -16,32 +16,26 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String Message = 'Unknown'; + late CIMSocket? cimSocket = null; + + late List list = []; + + late bool connectStatus = false; + @override void initState() { super.initState(); - initCimState(); - } - - final CIMSocket cimSocket = CIMSocket(); - // Platform messages are asynchronous, so we initialize in an async method. - Future initCimState() async { - // Platform messages may fail, so we use a try/catch PlatformException. - // We also handle the message potentially returning null. - try { - cimSocket.init('api.hoxin.farsunset.com', 34567, 16501516154949); - cimSocket.connect(); - cimSocket.addListener(() { - if (cimSocket.model == null) return; - setState(() { - Message = cimSocket.model!.toProto3Json().toString(); - }); + cimSocket = CIMSocket(onMessageReceived: (value) { + setState(() { + list.add(value.toProto3Json().toString()); }); - } on PlatformException {} - - // If the widget was removed from the tree while the asynchronous platform - // message was in flight, we want to discard the reply rather than calling - // setState to update our non-existent appearance. + }, onConnectionStatusChanged: (value) { + setState(() { + connectStatus = value; + }); + }); + cimSocket!.init('api.hoxin.farsunset.com', 34567, 16501516154949); + cimSocket!.connect(); } @override @@ -49,10 +43,13 @@ class _MyAppState extends State { return MaterialApp( home: Scaffold( appBar: AppBar( - title: const Text('Plugin example app'), + title: Text(connectStatus ? '已连接' : '未连接'), ), - body: Center( - child: Text('Message is: $Message\n'), + body: ListView.builder( + itemCount: list.length, + itemBuilder: (context, index) { + return Text(list[index]); + }, ), ), ); diff --git a/cim-client-sdk/cim-flutter-sdk/example/pubspec.lock b/cim-client-sdk/cim-flutter-sdk/example/pubspec.lock index 8e8f4d0..849852f 100644 --- a/cim-client-sdk/cim-flutter-sdk/example/pubspec.lock +++ b/cim-client-sdk/cim-flutter-sdk/example/pubspec.lock @@ -31,7 +31,7 @@ packages: path: ".." relative: true source: path - version: "1.0.2" + version: "1.0.3" clock: dependency: transitive description: diff --git a/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk.dart b/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk.dart deleted file mode 100644 index 9c9ccfd..0000000 --- a/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk.dart +++ /dev/null @@ -1,21 +0,0 @@ -// You have generated a new plugin project without specifying the `--platforms` -// flag. A plugin project with no platform support was generated. To add a -// platform, run `flutter create -t plugin --platforms .` under the -// same directory. You can also find a detailed instruction on how to add -// platforms in the `pubspec.yaml` at -// https://flutter.dev/docs/development/packages-and-plugins/developing-packages#plugin-platforms. - -import 'cim_socket.dart'; - -class CimFlutterSdk { - final CIMSocket cimSocket = CIMSocket(); - Future connect(String uri, int port, int uid) async { - cimSocket.init(uri, port, uid); - cimSocket.connect(); - return cimSocket; - } - - Future disconnect() async { - cimSocket.disConnect(); - } -} diff --git a/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_method_channel.dart b/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_method_channel.dart deleted file mode 100644 index f2be325..0000000 --- a/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_method_channel.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; - -import 'cim_flutter_sdk_platform_interface.dart'; - -/// An implementation of [CimFlutterSdkPlatform] that uses method channels. -class MethodChannelCimFlutterSdk extends CimFlutterSdkPlatform { - /// The method channel used to interact with the native platform. - @visibleForTesting - final methodChannel = const MethodChannel('cim_flutter_sdk'); - - @override - Future getPlatformVersion() async { - final version = await methodChannel.invokeMethod('getPlatformVersion'); - return version; - } -} diff --git a/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_platform_interface.dart b/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_platform_interface.dart deleted file mode 100644 index 46d6d9c..0000000 --- a/cim-client-sdk/cim-flutter-sdk/lib/cim_flutter_sdk_platform_interface.dart +++ /dev/null @@ -1,29 +0,0 @@ -import 'package:plugin_platform_interface/plugin_platform_interface.dart'; - -import 'cim_flutter_sdk_method_channel.dart'; - -abstract class CimFlutterSdkPlatform extends PlatformInterface { - /// Constructs a CimFlutterSdkPlatform. - CimFlutterSdkPlatform() : super(token: _token); - - static final Object _token = Object(); - - static CimFlutterSdkPlatform _instance = MethodChannelCimFlutterSdk(); - - /// The default instance of [CimFlutterSdkPlatform] to use. - /// - /// Defaults to [MethodChannelCimFlutterSdk]. - static CimFlutterSdkPlatform get instance => _instance; - - /// Platform-specific implementations should set this with their own - /// platform-specific class that extends [CimFlutterSdkPlatform] when - /// they register themselves. - static set instance(CimFlutterSdkPlatform instance) { - PlatformInterface.verifyToken(instance, _token); - _instance = instance; - } - - Future getPlatformVersion() { - throw UnimplementedError('platformVersion() has not been implemented.'); - } -} diff --git a/cim-client-sdk/cim-flutter-sdk/lib/cim_socket.dart b/cim-client-sdk/cim-flutter-sdk/lib/cim_socket.dart index 04b00d2..48b9eef 100644 --- a/cim-client-sdk/cim-flutter-sdk/lib/cim_socket.dart +++ b/cim-client-sdk/cim-flutter-sdk/lib/cim_socket.dart @@ -38,13 +38,19 @@ const DATA_HEADER_LENGTH = 1; const sOCKETAPPVERSION = '100'; -class CIMSocket extends ChangeNotifier { +class CIMSocket { + CIMSocket( + {required this.onMessageReceived, + required this.onConnectionStatusChanged}); + late Socket? socket; late String? uri; late int? port; late int? uid; late String endCode = "0"; final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin(); + final ValueChanged onMessageReceived; + final ValueChanged onConnectionStatusChanged; late TimerUtil timer = TimerUtil() ..mTotalTime = 10000 ..setOnTimerTickCallback((millisUntilFinished) { @@ -52,11 +58,6 @@ class CIMSocket extends ChangeNotifier { connect(); } }); - late bool isConnected = false; - - late messages.Model? _model = null; - - messages.Model? get model => _model; Future init(String uri, int port, int uid) async { this.uri = uri; @@ -84,33 +85,30 @@ class CIMSocket extends ChangeNotifier { var message = data.sublist(3, length + 3); messages.Model model = messages.Model(); model.mergeFromBuffer(message); - switchMessage(model); + onMessageReceived(model); } }, onError: (error, StackTrace trace) { socket = null; - isConnected = false; if (!timer.isActive()) { timer.setTotalTime(12000); timer.startCountDown(); } - notifyListeners(); + onConnectionStatusChanged(false); }, onDone: () { socket = null; - isConnected = false; - notifyListeners(); if (endCode != "999") { timer.setTotalTime(12000); timer.startCountDown(); } + onConnectionStatusChanged(false); }, cancelOnError: true); socket = sock; sendLoginMsg(); }).catchError((e) { socket = null; - isConnected = false; - notifyListeners(); timer.setTotalTime(12000); timer.startCountDown(); + onConnectionStatusChanged(false); }); } @@ -157,30 +155,10 @@ class CIMSocket extends ChangeNotifier { protobuf.setRange(3, data.length + 3, data); socket!.add(protobuf); await socket!.flush(); - isConnected = true; + onConnectionStatusChanged(true); if (timer.isActive()) { timer.cancel(); } - notifyListeners(); - } - - /// 发送群组聊天 tag - Future sendTag(String tag) async { - Map map1 = {"tag": tag}; - int time = DateTime.now().millisecondsSinceEpoch; - Int64 timeStamp = Int64.parseInt(time.toString()); - var body = sentbody.Model(data: map1); - body.key = "client_set_tag"; - body.timestamp = timeStamp; - var data = body.writeToBuffer(); - var protobuf = Uint8List(data.length + 3); - protobuf[0] = 3; - protobuf[1] = (data.length & 0xff); - protobuf[2] = ((data.length >> 8) & 0xff); - protobuf.setRange(3, data.length + 3, data); - socket!.add(protobuf); - await socket!.flush(); - return 2; } //发送PONG响应 @@ -197,17 +175,9 @@ class CIMSocket extends ChangeNotifier { pONG.setRange(3, 6, pONGBODY); socket!.add(pONG); await socket!.flush(); - isConnected = true; if (timer.isActive()) { timer.cancel(); } - notifyListeners(); - } - - ///消息分发器 - Future switchMessage(messages.Model model) async { - _model = model; - notifyListeners(); } } diff --git a/cim-client-sdk/cim-flutter-sdk/pubspec.yaml b/cim-client-sdk/cim-flutter-sdk/pubspec.yaml index 260105a..0af8d38 100644 --- a/cim-client-sdk/cim-flutter-sdk/pubspec.yaml +++ b/cim-client-sdk/cim-flutter-sdk/pubspec.yaml @@ -1,7 +1,7 @@ name: cim_flutter_sdk description: a flutter sdk for cim. repository: https://gitee.com/farsunset/cim -version: 1.0.2 +version: 1.0.3 homepage: http://farsunset.com/ environment: @@ -11,7 +11,6 @@ environment: dependencies: flutter: sdk: flutter - plugin_platform_interface: ^2.0.2 protobuf: ^2.0.1 # Google protobuf 序列化 device_info_plus: ^8.2.2 common_utils: ^2.1.0