接收消息
This commit is contained in:
parent
625579e510
commit
dc2acc7284
|
|
@ -1,4 +1,6 @@
|
|||
// 定义密钥
|
||||
import 'dart:convert';
|
||||
|
||||
List skey = [8, 1, 2, 5, 4];
|
||||
|
||||
// 单个字符异或运算
|
||||
|
|
@ -11,8 +13,8 @@ String makecodeChar(String c, int key, int key2) {
|
|||
// 单个字符解密
|
||||
String cutcodeChar(String c, int key, int key2) {
|
||||
int charCode = c.codeUnitAt(0);
|
||||
charCode = (((charCode - 1) ^ key) ^ key2) - key;
|
||||
return String.fromCharCode(charCode);
|
||||
int decryptedCharCode = (((charCode - 1) ^ key) ^ key2) - key;
|
||||
return String.fromCharCode(decryptedCharCode);
|
||||
}
|
||||
|
||||
// 加密
|
||||
|
|
@ -25,8 +27,13 @@ String makecode(String pstr, List pkey) {
|
|||
}
|
||||
|
||||
// 解密
|
||||
void cutecode(String pstr, List pkey) {
|
||||
for (int i = 0; i < pstr.length; i++) {
|
||||
pstr = pstr.replaceRange(i, i + 1, cutcodeChar(pstr[i], pkey[i % 5], pkey[(i + 18) % 5]));
|
||||
String cutecode(List<int> pstr) {
|
||||
print(pstr);
|
||||
int len = pstr.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
pstr[i] = cutcodeChar(String.fromCharCode(pstr[i])
|
||||
, skey[i % 5], skey[(i + 18) % 5]).codeUnitAt(0);
|
||||
}
|
||||
print(pstr);
|
||||
return String.fromCharCodes(pstr);
|
||||
}
|
||||
|
|
@ -26,34 +26,56 @@ class Socket {
|
|||
|
||||
RawDatagramSocket? socket;
|
||||
|
||||
// IC 0主机 1 从机
|
||||
int ic = 1;
|
||||
|
||||
connect() async {
|
||||
socket = await RawDatagramSocket.bind('127.0.0.2', 1);
|
||||
socket = await RawDatagramSocket.bind('127.0.0.3', 1);
|
||||
print('UDP Socket bound to ${socket?.address.address}:${socket?.port}');
|
||||
|
||||
// 发送身份包
|
||||
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;
|
||||
|
||||
print(d.data.toList());
|
||||
// String message = String.fromCharCodes(d.data).trim();
|
||||
// print('Datagram from ${d.address.address}:${d.port}: $message');
|
||||
|
||||
Map data = dataCute(d.data);
|
||||
|
||||
|
||||
});
|
||||
|
||||
heartbeat();
|
||||
}
|
||||
|
||||
/// 发送消息
|
||||
sendMessage(Map data){
|
||||
socket?.send(dataMake(data), InternetAddress(url), port);
|
||||
Map messageMap = {
|
||||
"op": 2,
|
||||
"value": data
|
||||
};
|
||||
socket?.send(dataMake(messageMap), InternetAddress(url), port);
|
||||
}
|
||||
|
||||
|
||||
/// 心跳
|
||||
heartbeat(){
|
||||
Timer timer = Timer(const Duration(seconds: 10), () {
|
||||
Timer timer = Timer(const Duration(seconds: 10), () async {
|
||||
|
||||
if (socket == null){
|
||||
// 重连
|
||||
await connect();
|
||||
}
|
||||
|
||||
Map data = {
|
||||
"op": 1,
|
||||
"IC": 0
|
||||
"IC": ic
|
||||
};
|
||||
socket?.send(dataMake(data), InternetAddress(url), port);
|
||||
|
||||
|
|
@ -74,4 +96,21 @@ class Socket {
|
|||
return uinData.toList();
|
||||
}
|
||||
|
||||
/// 接收数据处理
|
||||
Map dataCute(Uint8List data){
|
||||
|
||||
if (data.length <= 4) return {};
|
||||
|
||||
Uint8List pData = data.sublist(4,data.length);
|
||||
|
||||
// String ps = String.fromCharCodes(pData);
|
||||
|
||||
String str = cutecode(pData);
|
||||
print(str);
|
||||
|
||||
Map map = json.decode(str);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ class _WindowsPageState extends State<WindowsPage> {
|
|||
void initState() {
|
||||
super.initState();
|
||||
|
||||
SocketUtils.getInstance().connect();
|
||||
SocketUtils.getInstance().heartbeat();
|
||||
|
||||
controllerInit();
|
||||
}
|
||||
|
||||
|
|
@ -42,8 +45,6 @@ class _WindowsPageState extends State<WindowsPage> {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
SocketUtils.getInstance().connect();
|
||||
|
||||
if (initDone == false) return Container();
|
||||
|
||||
return Scaffold(
|
||||
|
|
|
|||
Loading…
Reference in New Issue