!16 修复 重复收到消息的bug

Merge pull request !16 from 杨杰/master
This commit is contained in:
远方夕阳 2023-07-10 10:35:08 +00:00 committed by Gitee
commit 5252c00a8f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
10 changed files with 67 additions and 154 deletions

View File

@ -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"}
{"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"}

View File

@ -1,3 +1,3 @@
## 1.0.2
## 1.0.3
* 修复version字段过长的错误
* 修复重复收到消息的BUG

View File

@ -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<String> list = [];
cimSocket.init('127.0.0.1', 34567, 16501516154949);
cimSocket.connect();
cimSocket.addListener(() {
if (cimSocket.model == null) return;
late bool connectStatus = false;
@override
void initState() {
super.initState();
cimSocket = CIMSocket(onMessageReceived: (value) {
setState(() {
Message = cimSocket.model!.toProto3Json().toString();
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]);
},
),
),
);
}
```

View File

@ -16,32 +16,26 @@ class MyApp extends StatefulWidget {
}
class _MyAppState extends State<MyApp> {
String Message = 'Unknown';
late CIMSocket? cimSocket = null;
late List<String> 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<void> 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;
cimSocket = CIMSocket(onMessageReceived: (value) {
setState(() {
Message = cimSocket.model!.toProto3Json().toString();
list.add(value.toProto3Json().toString());
});
}, onConnectionStatusChanged: (value) {
setState(() {
connectStatus = value;
});
});
} 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.
cimSocket!.init('api.hoxin.farsunset.com', 34567, 16501516154949);
cimSocket!.connect();
}
@override
@ -49,10 +43,13 @@ class _MyAppState extends State<MyApp> {
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]);
},
),
),
);

View File

@ -31,7 +31,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.2"
version: "1.0.3"
clock:
dependency: transitive
description:

View File

@ -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 <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<CIMSocket> connect(String uri, int port, int uid) async {
cimSocket.init(uri, port, uid);
cimSocket.connect();
return cimSocket;
}
Future<void> disconnect() async {
cimSocket.disConnect();
}
}

View File

@ -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<String?> getPlatformVersion() async {
final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
}
}

View File

@ -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<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.');
}
}

View File

@ -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<messages.Model> onMessageReceived;
final ValueChanged<bool> 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<int> sendTag(String tag) async {
Map<String, String> 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();
}
}

View File

@ -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