|
|
@ -6,7 +6,7 @@ |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
// 引用TCP插件
|
|
|
// 引用TCP插件
|
|
|
//const TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin');
|
|
|
|
|
|
|
|
|
const TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin'); |
|
|
// const TCPSocket = uni.requireNativePlugin("Aimer-TCPPlugin");
|
|
|
// const TCPSocket = uni.requireNativePlugin("Aimer-TCPPlugin");
|
|
|
|
|
|
|
|
|
// TCP连接配置
|
|
|
// TCP连接配置
|
|
|
@ -67,52 +67,52 @@ class TCPConnection { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('开始建立TCP连接:', connectionConfig); |
|
|
console.log('开始建立TCP连接:', connectionConfig); |
|
|
// TCPSocket.connect(
|
|
|
|
|
|
// connectionConfig,
|
|
|
|
|
|
// result => {
|
|
|
|
|
|
// /**
|
|
|
|
|
|
// * status : 0 连接成功
|
|
|
|
|
|
// * status : 1 断开连接
|
|
|
|
|
|
// * receivedMsg : 服务器返回字符串(普通的字符串交互)
|
|
|
|
|
|
// * receivedHexMsg : 服务器返回字节数组(单片机、智能家居等硬件数据交互)
|
|
|
|
|
|
// */
|
|
|
|
|
|
// if (result.status == '0') {
|
|
|
|
|
|
// // TCP连接成功
|
|
|
|
|
|
// this.channelConnections.set(connectionConfig.channel, true);
|
|
|
|
|
|
// console.log(`TCP连接成功 - Channel ${connectionConfig.channel}`);
|
|
|
|
|
|
// this._notifyConnectionCallbacks('connected', result, connectionConfig.channel);
|
|
|
|
|
|
// } else if (result.status == '1') {
|
|
|
|
|
|
// // TCP断开连接
|
|
|
|
|
|
// this.channelConnections.set(connectionConfig.channel, false);
|
|
|
|
|
|
// console.log(`TCP断开连接 - Channel ${connectionConfig.channel}`);
|
|
|
|
|
|
// this._notifyConnectionCallbacks('disconnected', result, connectionConfig.channel);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
TCPSocket.connect( |
|
|
|
|
|
connectionConfig, |
|
|
|
|
|
result => { |
|
|
|
|
|
/** |
|
|
|
|
|
* status : 0 连接成功 |
|
|
|
|
|
* status : 1 断开连接 |
|
|
|
|
|
* receivedMsg : 服务器返回字符串(普通的字符串交互) |
|
|
|
|
|
* receivedHexMsg : 服务器返回字节数组(单片机、智能家居等硬件数据交互) |
|
|
|
|
|
*/ |
|
|
|
|
|
if (result.status == '0') { |
|
|
|
|
|
// TCP连接成功
|
|
|
|
|
|
this.channelConnections.set(connectionConfig.channel, true); |
|
|
|
|
|
console.log(`TCP连接成功 - Channel ${connectionConfig.channel}`); |
|
|
|
|
|
this._notifyConnectionCallbacks('connected', result, connectionConfig.channel); |
|
|
|
|
|
} else if (result.status == '1') { |
|
|
|
|
|
// TCP断开连接
|
|
|
|
|
|
this.channelConnections.set(connectionConfig.channel, false); |
|
|
|
|
|
console.log(`TCP断开连接 - Channel ${connectionConfig.channel}`); |
|
|
|
|
|
this._notifyConnectionCallbacks('disconnected', result, connectionConfig.channel); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// if (result.receivedMsg) {
|
|
|
|
|
|
// // 服务器返回字符串
|
|
|
|
|
|
// console.log('收到字符串消息:', result.receivedMsg);
|
|
|
|
|
|
// this._notifyMessageCallbacks('string', result.receivedMsg, null, connectionConfig.channel);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
if (result.receivedMsg) { |
|
|
|
|
|
// 服务器返回字符串
|
|
|
|
|
|
console.log('收到字符串消息:', result.receivedMsg); |
|
|
|
|
|
this._notifyMessageCallbacks('string', result.receivedMsg, null, connectionConfig.channel); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// // if (result.receivedHexMsg) {
|
|
|
|
|
|
// // // 硬件服务器返回16进制数据
|
|
|
|
|
|
// // console.log('收到16进制消息:', result.receivedHexMsg);
|
|
|
|
|
|
// // let msg = result.receivedHexMsg;
|
|
|
|
|
|
// // let sum = msg.length / 2;
|
|
|
|
|
|
// // let arr = [];
|
|
|
|
|
|
// // for (let k = 0; k < sum; k++) {
|
|
|
|
|
|
// // let i = msg.substring(k * 2, k * 2 + 2);
|
|
|
|
|
|
// // arr.push(i);
|
|
|
|
|
|
// // }
|
|
|
|
|
|
// // console.log('解析后的16进制数组:', arr);
|
|
|
|
|
|
// // this._notifyMessageCallbacks('hex', result.receivedHexMsg, arr);
|
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
// if (result.receivedHexMsg) {
|
|
|
|
|
|
// // 硬件服务器返回16进制数据
|
|
|
|
|
|
// console.log('收到16进制消息:', result.receivedHexMsg);
|
|
|
|
|
|
// let msg = result.receivedHexMsg;
|
|
|
|
|
|
// let sum = msg.length / 2;
|
|
|
|
|
|
// let arr = [];
|
|
|
|
|
|
// for (let k = 0; k < sum; k++) {
|
|
|
|
|
|
// let i = msg.substring(k * 2, k * 2 + 2);
|
|
|
|
|
|
// arr.push(i);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// console.log('解析后的16进制数组:', arr);
|
|
|
|
|
|
// this._notifyMessageCallbacks('hex', result.receivedHexMsg, arr);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
// // 执行回调函数
|
|
|
|
|
|
// if (callback && typeof callback === "function") {
|
|
|
|
|
|
// callback(result);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
// 执行回调函数
|
|
|
|
|
|
if (callback && typeof callback === "function") { |
|
|
|
|
|
callback(result); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -144,7 +144,7 @@ class TCPConnection { |
|
|
sendConfig.charsetname = config.charsetname; |
|
|
sendConfig.charsetname = config.charsetname; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//TCPSocket.send(sendConfig);
|
|
|
|
|
|
|
|
|
TCPSocket.send(sendConfig); |
|
|
console.log("js成功发送TCP消息:", messageStr); |
|
|
console.log("js成功发送TCP消息:", messageStr); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
@ -159,7 +159,7 @@ class TCPConnection { |
|
|
channel: channel |
|
|
channel: channel |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
//TCPSocket.disconnect(disconnectConfig);
|
|
|
|
|
|
|
|
|
TCPSocket.disconnect(disconnectConfig); |
|
|
this.channelConnections.set(channel, false); |
|
|
this.channelConnections.set(channel, false); |
|
|
console.log(`TCP连接已断开 - Channel ${channel}`, disconnectConfig); |
|
|
console.log(`TCP连接已断开 - Channel ${channel}`, disconnectConfig); |
|
|
} |
|
|
} |
|
|
|