Compare commits
	
		
			4 Commits
		
	
	
		
			e91a3a40af
			...
			ff882a5e06
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | ff882a5e06 | |
|  | 30af057969 | |
|  | 51d33a1137 | |
|  | 784cad7f3d | 
|  | @ -1,9 +1,12 @@ | ||||||
| 
 | 
 | ||||||
| import 'dart:async'; | import 'dart:async'; | ||||||
| import 'dart:convert'; | import 'dart:convert'; | ||||||
|  | import 'dart:ffi'; | ||||||
| import 'dart:io'; | import 'dart:io'; | ||||||
|  | import 'dart:typed_data'; | ||||||
| 
 | 
 | ||||||
| import 'package:common/utils/toast_utils.dart'; | import 'package:common/utils/toast_utils.dart'; | ||||||
|  | import 'package:web_synchronization_tool/windows/little_extension.dart'; | ||||||
| import 'package:web_synchronization_tool/windows/socket_tool.dart'; | import 'package:web_synchronization_tool/windows/socket_tool.dart'; | ||||||
| 
 | 
 | ||||||
| import '../windows/code.dart'; | import '../windows/code.dart'; | ||||||
|  | @ -29,26 +32,24 @@ typedef loginBlockFun = Function(Map); | ||||||
| 
 | 
 | ||||||
| class LoginSocket{ | class LoginSocket{ | ||||||
| 
 | 
 | ||||||
|   String url = '192.168.200.17'; |   String url = '110.42.251.214'; | ||||||
|   static int port = 37785; |   static int port = 37785; | ||||||
| 
 | 
 | ||||||
|   String uuid = ''; |   String uuid = ''; | ||||||
| 
 | 
 | ||||||
|   RawDatagramSocket? socket; |   Socket? socket; | ||||||
| 
 | 
 | ||||||
|   int heartTime = 0; |   int heartTime = 0; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|   connect() async { |   connect() async { | ||||||
|     socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0); |     socket?.close(); | ||||||
|  |     socket = await Socket.connect(url, port, timeout: const Duration(seconds: 30)); | ||||||
|  | 
 | ||||||
|  |     socket?.listen((event) { | ||||||
|  | 
 | ||||||
|  |       Map data = dataCute(event); | ||||||
| 
 | 
 | ||||||
|     // 监听来自网络的数据包 |  | ||||||
|     socket?.listen((RawSocketEvent e) { |  | ||||||
|       Datagram? d = socket?.receive(); |  | ||||||
|       if (d == null) return; |  | ||||||
|       print(d.data); |  | ||||||
|       Map data = json.decode(cutecode(d.data));// dataCute(); |  | ||||||
|       print(data); |  | ||||||
|       // 登录结果 |       // 登录结果 | ||||||
|       if (data['op'] == 2){ |       if (data['op'] == 2){ | ||||||
|         loginBlock(data); |         loginBlock(data); | ||||||
|  | @ -57,7 +58,9 @@ class LoginSocket{ | ||||||
|       // 服务器心跳 |       // 服务器心跳 | ||||||
|       if (data['op'] == 4){ |       if (data['op'] == 4){ | ||||||
|         try{ |         try{ | ||||||
|           if(data['time']> (3 * 60 * 1000) ){ // 时间戳 > 3分钟 |           int time = data['time']; | ||||||
|  |           time = DateTime.now().millisecondsSinceEpoch - time; | ||||||
|  |           if(time> (3 * 60 * 1000) ){ // 时间戳 > 3分钟 | ||||||
|             socketError(); |             socketError(); | ||||||
|           } |           } | ||||||
|         }catch(e){ |         }catch(e){ | ||||||
|  | @ -65,19 +68,16 @@ class LoginSocket{ | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|     }); |     },onDone: (){ | ||||||
| 
 |     },onError: (error){ | ||||||
|     // 设置超时时间为30秒 |       // 连接断开 | ||||||
|     socket?.timeout(const Duration(seconds: 30), onTimeout: (eventSink) { |       socketError(); | ||||||
| 
 |       socket?.destroy(); | ||||||
|       ToastUtils.dismissLoading(); |  | ||||||
|       ToastUtils.showToast('连接超时'); |  | ||||||
| 
 |  | ||||||
|       eventSink.close(); // 关闭套接字 |  | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|   socketError(){ |   socketError(){ | ||||||
|     /// 任何错误都显示这个 |     /// 任何错误都显示这个 | ||||||
|     ToastUtils.showLoading(msg: '重连中'); |     ToastUtils.showLoading(msg: '重连中'); | ||||||
|  | @ -88,17 +88,21 @@ class LoginSocket{ | ||||||
| 
 | 
 | ||||||
|   late loginBlockFun loginBlock; |   late loginBlockFun loginBlock; | ||||||
| 
 | 
 | ||||||
|  |   // 登录 | ||||||
|   login(String account,String passwoord,loginBlockFun blockFun){ |   login(String account,String passwoord,loginBlockFun blockFun){ | ||||||
| 
 | 
 | ||||||
|     loginBlock = blockFun; |     loginBlock = blockFun; | ||||||
| 
 | 
 | ||||||
|     Map map = { |     Map map = { | ||||||
|  |       'op':1, | ||||||
|       'account':account, |       'account':account, | ||||||
|       'passwoord':passwoord, |       'password':passwoord, | ||||||
|       'uuid':uuid // 默认0, 需要c盘 硬盘序列号 |       'uuid':uuid | ||||||
|     }; |     }; | ||||||
|     List<int> data = jsonEncode(map).codeUnits; | 
 | ||||||
|     socket?.send(data, InternetAddress(url), port); |     final dd = dataMake(map); | ||||||
|  |     socket?.add(dd); | ||||||
|  |     socket?.flush(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /// 心跳 |   /// 心跳 | ||||||
|  | @ -107,7 +111,7 @@ class LoginSocket{ | ||||||
|     int timestamp = now.millisecondsSinceEpoch; // 秒 |     int timestamp = now.millisecondsSinceEpoch; // 秒 | ||||||
|     heartTime = timestamp; |     heartTime = timestamp; | ||||||
| 
 | 
 | ||||||
|     Timer timer = Timer(const Duration(seconds: 20), () async { |     Timer(const Duration(seconds: 20), () async { | ||||||
| 
 | 
 | ||||||
|       if (socket == null){ |       if (socket == null){ | ||||||
|         // 重连 |         // 重连 | ||||||
|  | @ -117,40 +121,44 @@ class LoginSocket{ | ||||||
|       Map map =   { |       Map map =   { | ||||||
|         "op": 3, |         "op": 3, | ||||||
|       }; |       }; | ||||||
|       List<int> data = jsonEncode(map).codeUnits; | 
 | ||||||
|       socket?.send(data, InternetAddress(url), port); |       socket?.add(dataMake(map)); | ||||||
|  |       socket?.flush(); | ||||||
| 
 | 
 | ||||||
|       heartbeat(); |       heartbeat(); | ||||||
| 
 | 
 | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   // /// 发送数据的处理 | 
 | ||||||
|   // List<int> dataMake(Map map){ |   /// 发送数据的处理 | ||||||
|   //   // 将Map对象转换为JSON字符串 |   List<int> dataMake(Map map){ | ||||||
|   //   String json = jsonEncode(map); | 
 | ||||||
|   //   // 加密 | 
 | ||||||
|   //   String ps = makecode(json, skey); |     // 将Map对象转换为JSON字符串 | ||||||
|   //   // 转成无符号整数数组 |     String json = jsonEncode(map); | ||||||
|   //   Uint8List uinData = Uint8List.fromList(ps.codeUnits); |     // 加密 | ||||||
|   //   // 在前面添加 4位 表示长度的小端序 |     String ps = makecode(json, skey); | ||||||
|   //   uinData = uinData.toLittle(value: uinData.length); | 
 | ||||||
|   //   return uinData.toList(); |     List<int> bytes = ps.codeUnits; // 将字符串转换为字节数组 | ||||||
|   // } | 
 | ||||||
|   // |     return bytes.toLittle(); | ||||||
|   // /// 接收数据处理 |   } | ||||||
|   // Map dataCute(Uint8List data){ | 
 | ||||||
|   // |   /// 接收数据处理 | ||||||
|   //   if (data.length <= 4) return {}; |   Map dataCute(Uint8List data){ | ||||||
|   // | 
 | ||||||
|   //   Uint8List pData = data.sublist(4,data.length); |     if (data.length <= 4) return {}; | ||||||
|   // | 
 | ||||||
|   //   String str = cutecode(pData); |     Uint8List pData = data.sublist(4,data.length); | ||||||
|   // | 
 | ||||||
|   //   Map map = json.decode(str); |     String str = cutecode(pData); | ||||||
|   // | 
 | ||||||
|   //   return map; |     Map map = json.decode(str); | ||||||
|   // } | 
 | ||||||
|  |     return map; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -82,8 +82,6 @@ class _AccountNumberLoginWidgetState extends State<AccountNumberLoginWidget> { | ||||||
|           _login(); |           _login(); | ||||||
|         },), |         },), | ||||||
|         WidgetUtils.spacer(height: 15), |         WidgetUtils.spacer(height: 15), | ||||||
|         const Text('注册请直接输入账号密码 再点击注册',style: TextStyle(fontSize: 14,color: Colors.grey),), |  | ||||||
|         WidgetUtils.spacer(height: 15), |  | ||||||
|         TextButton( |         TextButton( | ||||||
|           onPressed: _login, |           onPressed: _login, | ||||||
|           style: TextButton.styleFrom(padding: EdgeInsets.zero), |           style: TextButton.styleFrom(padding: EdgeInsets.zero), | ||||||
|  |  | ||||||
|  | @ -41,7 +41,10 @@ class _MyAppState extends State<MyApp> { | ||||||
| 
 | 
 | ||||||
|   info() async { |   info() async { | ||||||
|     final deviceInfo = await DeviceInfoPlugin().deviceInfo; |     final deviceInfo = await DeviceInfoPlugin().deviceInfo; | ||||||
|     LoginSocketUtils.getInstance().uuid = deviceInfo.data.toString(); |     String uuid = deviceInfo.data['deviceId']; | ||||||
|  |     uuid = uuid.replaceAll('{', ''); | ||||||
|  |     uuid = uuid.replaceAll('}', ''); | ||||||
|  |     LoginSocketUtils.getInstance().uuid = uuid; | ||||||
| 
 | 
 | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -23,6 +23,30 @@ extension MapLittle on Map { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | extension ListIntLittle on List<int> { | ||||||
|  | 
 | ||||||
|  |   List<int> toLittle({int? value, int length = 4}){ | ||||||
|  | 
 | ||||||
|  |     List<int> ret = []; | ||||||
|  |     /// 初始化 定义数据长度 | ||||||
|  |     final little = ByteData(4); | ||||||
|  |     /// 设置小端序数据 | ||||||
|  |     little.setInt32(0, value ?? this.length, Endian.little); | ||||||
|  | 
 | ||||||
|  |     for (int i = 0; i < little.lengthInBytes; i++) { | ||||||
|  |       ret.add(little.getUint8(i)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     ret.addAll(this); | ||||||
|  | 
 | ||||||
|  |     return ret; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| extension Uint8ListLittle on Uint8List { | extension Uint8ListLittle on Uint8List { | ||||||
| 
 | 
 | ||||||
|   /// 在转成 Uint8List 的数据前面添加 小端序 |   /// 在转成 Uint8List 的数据前面添加 小端序 | ||||||
|  |  | ||||||
|  | @ -8,7 +8,7 @@ import 'little_extension.dart'; | ||||||
| import 'code.dart'; | import 'code.dart'; | ||||||
| import 'number_tool.dart'; | import 'number_tool.dart'; | ||||||
| 
 | 
 | ||||||
| class SocketUtils extends Socket{ | class SocketUtils extends SyncSocket{ | ||||||
|   // 私有构造函数 |   // 私有构造函数 | ||||||
|   SocketUtils._(); |   SocketUtils._(); | ||||||
|   // 私有静态变量,保存类的唯一实例 |   // 私有静态变量,保存类的唯一实例 | ||||||
|  | @ -22,7 +22,7 @@ class SocketUtils extends Socket{ | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class Socket { | class SyncSocket { | ||||||
| 
 | 
 | ||||||
|   String url = '127.0.0.1'; |   String url = '127.0.0.1'; | ||||||
|   static int port = 12345; |   static int port = 12345; | ||||||
|  | @ -30,12 +30,13 @@ class Socket { | ||||||
|   RawDatagramSocket? socket; |   RawDatagramSocket? socket; | ||||||
| 
 | 
 | ||||||
|   // IC 0主机 1 从机 |   // IC 0主机 1 从机 | ||||||
|   int ic = 1; |   int ic = 0; | ||||||
| 
 | 
 | ||||||
|   /// 从机ip |   /// 从机ip | ||||||
|   List<String> childrenIp = []; |   List<String> childrenIp = []; | ||||||
| 
 | 
 | ||||||
|   connect() async { |   connect() async { | ||||||
|  |     socket?.close(); | ||||||
|     socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0); |     socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0); | ||||||
| 
 | 
 | ||||||
|     // 发送身份包 |     // 发送身份包 | ||||||
|  |  | ||||||
|  | @ -152,11 +152,6 @@ class _ShowWebWidgetState extends State<ShowWebWidget> { | ||||||
|     if (widget.main == false) return input(); |     if (widget.main == false) return input(); | ||||||
|     return Row( |     return Row( | ||||||
|       children: [ |       children: [ | ||||||
|         TextButton( |  | ||||||
|             onPressed: () { |  | ||||||
|               widget.controller.openDevTools(); |  | ||||||
|             }, |  | ||||||
|             child: const Text('开发者')), |  | ||||||
|         inputNumber(), |         inputNumber(), | ||||||
|         Row( |         Row( | ||||||
|           children: [ |           children: [ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue