1 changed files with 223 additions and 0 deletions
@ -0,0 +1,223 @@ |
|||
/** |
|||
* TCP连接工具类 |
|||
* 用于处理TCP连接、发送消息和断开连接 |
|||
*/ |
|||
|
|||
// 引用TCP插件
|
|||
const TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin'); |
|||
|
|||
// TCP连接配置
|
|||
const TCP_CONFIG = { |
|||
ip: '192.168.1.9', |
|||
port: '8080', |
|||
channel: '1', // 可选 1~20
|
|||
charsetname: 'UTF-8' // 默认UTF-8,可选GBK
|
|||
}; |
|||
|
|||
/** |
|||
* TCP连接管理类 |
|||
*/ |
|||
class TCPConnection { |
|||
constructor() { |
|||
this.isConnected = false; |
|||
this.connectionCallbacks = []; |
|||
this.messageCallbacks = []; |
|||
} |
|||
|
|||
/** |
|||
* TCP初始化连接 |
|||
* @param {Object} config - 连接配置 {ip, port, channel, charsetname} |
|||
* @param {Function} callback - 连接状态回调函数 |
|||
*/ |
|||
connect(config = {}, callback = null) { |
|||
const connectionConfig = { |
|||
channel: config.channel || TCP_CONFIG.channel, |
|||
ip: config.ip || TCP_CONFIG.ip, |
|||
port: config.port || TCP_CONFIG.port |
|||
}; |
|||
|
|||
// 如果指定了字符集,添加到配置中
|
|||
if (config.charsetname || TCP_CONFIG.charsetname) { |
|||
connectionConfig.charsetname = config.charsetname || TCP_CONFIG.charsetname; |
|||
} |
|||
|
|||
TCPSocket.connect( |
|||
connectionConfig, |
|||
result => { |
|||
/** |
|||
* status : 0 连接成功 |
|||
* status : 1 断开连接 |
|||
* receivedMsg : 服务器返回字符串(普通的字符串交互) |
|||
* receivedHexMsg : 服务器返回字节数组(单片机、智能家居等硬件数据交互) |
|||
*/ |
|||
if (result.status == '0') { |
|||
// TCP连接成功
|
|||
this.isConnected = true; |
|||
console.log('TCP连接成功'); |
|||
this._notifyConnectionCallbacks('connected', result); |
|||
} else if (result.status == '1') { |
|||
// TCP断开连接
|
|||
this.isConnected = false; |
|||
console.log('TCP断开连接'); |
|||
this._notifyConnectionCallbacks('disconnected', result); |
|||
} |
|||
|
|||
if (result.receivedMsg) { |
|||
// 服务器返回字符串
|
|||
console.log('收到字符串消息:', result.receivedMsg); |
|||
this._notifyMessageCallbacks('string', result.receivedMsg); |
|||
} |
|||
|
|||
// 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); |
|||
} |
|||
} |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* TCP发送消息(普通的字符串交互) |
|||
* @param {String|Object} message - 要发送的消息,如果是对象会自动转换为JSON字符串 |
|||
* @param {Object} config - 发送配置 {channel, charsetname} |
|||
*/ |
|||
send(message, config = {}) { |
|||
if (!this.isConnected) { |
|||
console.warn('TCP未连接,无法发送消息'); |
|||
return false; |
|||
} |
|||
|
|||
// 如果message是对象,转换为JSON字符串
|
|||
let messageStr = message; |
|||
if (typeof message === 'object') { |
|||
messageStr = JSON.stringify(message) + '\n'; |
|||
} |
|||
|
|||
const sendConfig = { |
|||
channel: config.channel || '1', // 注意:channel应该是字符串
|
|||
message: messageStr |
|||
}; |
|||
|
|||
// 如果指定了字符编码,添加到配置中
|
|||
if (config.charsetname) { |
|||
sendConfig.charsetname = config.charsetname; |
|||
} |
|||
|
|||
TCPSocket.send(sendConfig); |
|||
console.log('js成功发送TCP消息:', messageStr); |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* TCP断开连接 |
|||
* @param {Object} config - 断开配置 {channel} |
|||
*/ |
|||
disconnect(config = {}) { |
|||
const disconnectConfig = { |
|||
channel: config.channel || TCP_CONFIG.channel |
|||
}; |
|||
|
|||
TCPSocket.disconnect(disconnectConfig); |
|||
this.isConnected = false; |
|||
console.log('TCP连接已断开', disconnectConfig); |
|||
} |
|||
|
|||
/** |
|||
* 添加连接状态监听器 |
|||
* @param {Function} callback - 回调函数 (status, result) => {} |
|||
*/ |
|||
onConnectionChange(callback) { |
|||
if (typeof callback === 'function') { |
|||
this.connectionCallbacks.push(callback); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 添加消息监听器 |
|||
* @param {Function} callback - 回调函数 (type, message, parsedArray) => {} |
|||
*/ |
|||
onMessage(callback) { |
|||
if (typeof callback === 'function') { |
|||
this.messageCallbacks.push(callback); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 移除连接状态监听器 |
|||
* @param {Function} callback - 要移除的回调函数 |
|||
*/ |
|||
removeConnectionListener(callback) { |
|||
const index = this.connectionCallbacks.indexOf(callback); |
|||
if (index > -1) { |
|||
this.connectionCallbacks.splice(index, 1); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 移除消息监听器 |
|||
* @param {Function} callback - 要移除的回调函数 |
|||
*/ |
|||
removeMessageListener(callback) { |
|||
const index = this.messageCallbacks.indexOf(callback); |
|||
if (index > -1) { |
|||
this.messageCallbacks.splice(index, 1); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取连接状态 |
|||
* @returns {Boolean} 连接状态 |
|||
*/ |
|||
getConnectionStatus() { |
|||
return this.isConnected; |
|||
} |
|||
|
|||
/** |
|||
* 通知连接状态回调 |
|||
* @private |
|||
*/ |
|||
_notifyConnectionCallbacks(status, result) { |
|||
this.connectionCallbacks.forEach(callback => { |
|||
try { |
|||
callback(status, result); |
|||
} catch (error) { |
|||
console.error('连接状态回调执行错误:', error); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 通知消息回调 |
|||
* @private |
|||
*/ |
|||
_notifyMessageCallbacks(type, message, parsedArray = null) { |
|||
this.messageCallbacks.forEach(callback => { |
|||
try { |
|||
callback(type, message, parsedArray); |
|||
} catch (error) { |
|||
console.error('消息回调执行错误:', error); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
// 创建TCP连接实例
|
|||
const tcpConnection = new TCPConnection(); |
|||
|
|||
// 导出TCP连接实例和类
|
|||
export default tcpConnection; |
|||
export { TCPConnection, TCP_CONFIG }; |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue