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