133 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
| import 'dart:async';
 | |
| import 'dart:convert';
 | |
| import 'dart:io';
 | |
| import 'dart:typed_data';
 | |
| import 'package:web_synchronization_tool/windows/synchronization_web_tool.dart';
 | |
| 
 | |
| import 'little_extension.dart';
 | |
| import 'code.dart';
 | |
| import 'number_tool.dart';
 | |
| 
 | |
| class SocketUtils extends SyncSocket{
 | |
|   // 私有构造函数
 | |
|   SocketUtils._();
 | |
|   // 私有静态变量,保存类的唯一实例
 | |
|   static SocketUtils? _instance;
 | |
| 
 | |
|   // 公开的静态方法,返回类的唯一实例
 | |
|   static SocketUtils getInstance() {
 | |
|     _instance ??= SocketUtils._();
 | |
|     return _instance!;
 | |
|   }
 | |
| 
 | |
| }
 | |
| 
 | |
| class SyncSocket {
 | |
| 
 | |
|   String url = '127.0.0.1';
 | |
|   static int port = 46182;
 | |
| 
 | |
|   RawDatagramSocket? socket;
 | |
| 
 | |
|   // IC 0主机 1 从机
 | |
|   int ic = 1;
 | |
| 
 | |
|   /// 从机ip
 | |
|   List<String> childrenIp = [];
 | |
| 
 | |
|   connect() async {
 | |
|     socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
 | |
| 
 | |
|     // 发送身份包
 | |
|     Map data = {
 | |
|       "op": 0,
 | |
|       "IC": ic
 | |
|     };
 | |
|     socket?.send(dataMake(data), InternetAddress(url), port);
 | |
| 
 | |
|     // 监听来自网络的数据包
 | |
|     socket?.listen((RawSocketEvent e) {
 | |
|       Datagram? d = socket?.receive();
 | |
|       if (d == null) return;
 | |
| 
 | |
|       Map data = dataCute(d.data);
 | |
| 
 | |
|       // 收到消息
 | |
|       if (data['op'] == 2){
 | |
| 
 | |
|         print(data);
 | |
| 
 | |
|         //点击
 | |
|         if (data['click'] != null){
 | |
|           Map click = data['click'];
 | |
|           int x = click['x'] as int;
 | |
|           int y = click['y'] as int;
 | |
| 
 | |
|           SynchronizationWebTool.getInstance().clickSynchronization(x, y);
 | |
|         }
 | |
|         //输入
 | |
|         if (data['input'] != null){
 | |
|           int input = data['input'];
 | |
|           List<int> nums = NumberTool().randomNum(input);
 | |
|           SynchronizationWebTool.getInstance().input(nums);
 | |
|         }
 | |
| 
 | |
|       }
 | |
| 
 | |
|       // 心跳
 | |
|       if (data['op'] == 11){
 | |
| 
 | |
|       }
 | |
| 
 | |
|     });
 | |
| 
 | |
|   }
 | |
| 
 | |
|   /// 心跳
 | |
|   heartbeat(){
 | |
|     Timer timer = Timer(const Duration(seconds: 10), () async {
 | |
| 
 | |
|       if (socket == null){
 | |
|         // 重连
 | |
|         await connect();
 | |
|       }
 | |
| 
 | |
|       Map data =   {
 | |
|         "op": 1,
 | |
|         "IC": ic
 | |
|       };
 | |
|       socket?.send(dataMake(data), InternetAddress(url), port);
 | |
| 
 | |
|       heartbeat();
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   /// 发送数据的处理
 | |
|   List<int> dataMake(Map map){
 | |
|     // 将Map对象转换为JSON字符串
 | |
|     String json = jsonEncode(map);
 | |
|     // 加密
 | |
|     String ps = makecode(json, skey);
 | |
|     // 转成无符号整数数组
 | |
|     Uint8List uinData = Uint8List.fromList(ps.codeUnits);
 | |
|     // 在前面添加 4位 表示长度的小端序
 | |
|     uinData = uinData.toLittle(value: uinData.length);
 | |
|     return uinData.toList();
 | |
|   }
 | |
| 
 | |
|   /// 接收数据处理
 | |
|   Map dataCute(Uint8List data){
 | |
| 
 | |
|     if (data.length <= 4) return {};
 | |
| 
 | |
|     Uint8List pData = data.sublist(4,data.length);
 | |
| 
 | |
|     String str = cutecode(pData);
 | |
| 
 | |
|     Map map = json.decode(str);
 | |
| 
 | |
|     return map;
 | |
|   }
 | |
| 
 | |
| }
 |