157 lines
3.4 KiB
Dart
157 lines
3.4 KiB
Dart
|
|
|
|||
|
|
import 'dart:async';
|
|||
|
|
import 'dart:convert';
|
|||
|
|
import 'dart:io';
|
|||
|
|
|
|||
|
|
import 'package:common/utils/toast_utils.dart';
|
|||
|
|
import 'package:web_synchronization_tool/windows/socket_tool.dart';
|
|||
|
|
|
|||
|
|
import '../windows/code.dart';
|
|||
|
|
|
|||
|
|
class LoginSocketUtils extends LoginSocket {
|
|||
|
|
|
|||
|
|
// 私有构造函数
|
|||
|
|
LoginSocketUtils._();
|
|||
|
|
// 私有静态变量,保存类的唯一实例
|
|||
|
|
static LoginSocketUtils? _instance;
|
|||
|
|
|
|||
|
|
// 公开的静态方法,返回类的唯一实例
|
|||
|
|
static LoginSocketUtils getInstance() {
|
|||
|
|
_instance ??= LoginSocketUtils._();
|
|||
|
|
return _instance!;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
typedef loginBlockFun = Function(Map);
|
|||
|
|
|
|||
|
|
|
|||
|
|
class LoginSocket{
|
|||
|
|
|
|||
|
|
String url = '192.168.200.17';
|
|||
|
|
static int port = 37785;
|
|||
|
|
|
|||
|
|
String uuid = '';
|
|||
|
|
|
|||
|
|
RawDatagramSocket? socket;
|
|||
|
|
|
|||
|
|
int heartTime = 0;
|
|||
|
|
|
|||
|
|
|
|||
|
|
connect() async {
|
|||
|
|
socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
|
|||
|
|
|
|||
|
|
// 监听来自网络的数据包
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 服务器心跳
|
|||
|
|
if (data['op'] == 4){
|
|||
|
|
try{
|
|||
|
|
if(data['time']> (3 * 60 * 1000) ){ // 时间戳 > 3分钟
|
|||
|
|
socketError();
|
|||
|
|
}
|
|||
|
|
}catch(e){
|
|||
|
|
socketError();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 设置超时时间为30秒
|
|||
|
|
socket?.timeout(const Duration(seconds: 30), onTimeout: (eventSink) {
|
|||
|
|
|
|||
|
|
ToastUtils.dismissLoading();
|
|||
|
|
ToastUtils.showToast('连接超时');
|
|||
|
|
|
|||
|
|
eventSink.close(); // 关闭套接字
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
socketError(){
|
|||
|
|
/// 任何错误都显示这个
|
|||
|
|
ToastUtils.showLoading(msg: '重连中');
|
|||
|
|
/// 关闭同步 socket
|
|||
|
|
SocketUtils.getInstance().socket?.close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
late loginBlockFun loginBlock;
|
|||
|
|
|
|||
|
|
login(String account,String passwoord,loginBlockFun blockFun){
|
|||
|
|
|
|||
|
|
loginBlock = blockFun;
|
|||
|
|
|
|||
|
|
Map map = {
|
|||
|
|
'account':account,
|
|||
|
|
'passwoord':passwoord,
|
|||
|
|
'uuid':uuid // 默认0, 需要c盘 硬盘序列号
|
|||
|
|
};
|
|||
|
|
List<int> data = jsonEncode(map).codeUnits;
|
|||
|
|
socket?.send(data, InternetAddress(url), port);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 心跳
|
|||
|
|
heartbeat(){
|
|||
|
|
DateTime now = DateTime.now();
|
|||
|
|
int timestamp = now.millisecondsSinceEpoch; // 秒
|
|||
|
|
heartTime = timestamp;
|
|||
|
|
|
|||
|
|
Timer timer = Timer(const Duration(seconds: 20), () async {
|
|||
|
|
|
|||
|
|
if (socket == null){
|
|||
|
|
// 重连
|
|||
|
|
await connect();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Map map = {
|
|||
|
|
"op": 3,
|
|||
|
|
};
|
|||
|
|
List<int> data = jsonEncode(map).codeUnits;
|
|||
|
|
socket?.send(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;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|