You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
8.5 KiB

  1. /**
  2. * TCP连接工具类
  3. * 用于处理TCP连接发送消息和断开连接
  4. */
  5. // 引用TCP插件
  6. const TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin');
  7. // TCP连接配置
  8. const TCP_CONFIG = {
  9. ip: '192.168.1.9',
  10. port: '8080',
  11. channel: '1', // 可选 1~20
  12. charsetname: 'UTF-8' // 默认UTF-8,可选GBK
  13. };
  14. /**
  15. * TCP连接管理类
  16. */
  17. class TCPConnection {
  18. constructor() {
  19. this.channelConnections = new Map(); // 存储每个channel的连接状态
  20. this.connectionCallbacks = [];
  21. this.messageCallbacks = [];
  22. }
  23. /**
  24. * TCP初始化连接
  25. * @param {Object} config - 连接配置 {ip, port, channel, charsetname}
  26. * @param {Function} callback - 连接状态回调函数
  27. */
  28. connect(config = {}, callback = null) {
  29. const channel = config.channel || TCP_CONFIG.channel;
  30. // 如果该channel已经连接,先断开现有连接
  31. if (this.channelConnections.get(channel)) {
  32. console.log(`检测到channel ${channel}现有TCP连接,先断开...`);
  33. this.disconnect(config);
  34. // 等待断开完成后再连接
  35. setTimeout(() => {
  36. this._performConnect(config, callback);
  37. }, 300);
  38. } else {
  39. // 直接连接
  40. this._performConnect(config, callback);
  41. }
  42. }
  43. /**
  44. * 执行TCP连接
  45. * @param {Object} config - 连接配置
  46. * @param {Function} callback - 连接状态回调函数
  47. */
  48. _performConnect(config = {}, callback = null) {
  49. const connectionConfig = {
  50. channel: config.channel || TCP_CONFIG.channel,
  51. ip: config.ip || TCP_CONFIG.ip,
  52. port: config.port || TCP_CONFIG.port
  53. };
  54. // 如果指定了字符集,添加到配置中
  55. if (config.charsetname || TCP_CONFIG.charsetname) {
  56. connectionConfig.charsetname = config.charsetname || TCP_CONFIG.charsetname;
  57. }
  58. console.log('开始建立TCP连接:', connectionConfig);
  59. TCPSocket.connect(
  60. connectionConfig,
  61. result => {
  62. /**
  63. * status : 0 连接成功
  64. * status : 1 断开连接
  65. * receivedMsg : 服务器返回字符串(普通的字符串交互)
  66. * receivedHexMsg : 服务器返回字节数组(单片机智能家居等硬件数据交互)
  67. */
  68. if (result.status == '0') {
  69. // TCP连接成功
  70. this.channelConnections.set(connectionConfig.channel, true);
  71. console.log(`TCP连接成功 - Channel ${connectionConfig.channel}`);
  72. this._notifyConnectionCallbacks('connected', result, connectionConfig.channel);
  73. } else if (result.status == '1') {
  74. // TCP断开连接
  75. this.channelConnections.set(connectionConfig.channel, false);
  76. console.log(`TCP断开连接 - Channel ${connectionConfig.channel}`);
  77. this._notifyConnectionCallbacks('disconnected', result, connectionConfig.channel);
  78. }
  79. if (result.receivedMsg) {
  80. // 服务器返回字符串
  81. console.log('收到字符串消息:', result.receivedMsg);
  82. this._notifyMessageCallbacks('string', result.receivedMsg, null, connectionConfig.channel);
  83. }
  84. // if (result.receivedHexMsg) {
  85. // // 硬件服务器返回16进制数据
  86. // console.log('收到16进制消息:', result.receivedHexMsg);
  87. // let msg = result.receivedHexMsg;
  88. // let sum = msg.length / 2;
  89. // let arr = [];
  90. // for (let k = 0; k < sum; k++) {
  91. // let i = msg.substring(k * 2, k * 2 + 2);
  92. // arr.push(i);
  93. // }
  94. // console.log('解析后的16进制数组:', arr);
  95. // this._notifyMessageCallbacks('hex', result.receivedHexMsg, arr);
  96. // }
  97. // 执行回调函数
  98. if (callback && typeof callback === 'function') {
  99. callback(result);
  100. }
  101. }
  102. );
  103. }
  104. /**
  105. * TCP发送消息(普通的字符串交互)
  106. * @param {String|Object} message - 要发送的消息如果是对象会自动转换为JSON字符串
  107. * @param {Object} config - 发送配置 {channel, charsetname}
  108. */
  109. send(message, config = {}) {
  110. const channel = config.channel || '1';
  111. if (!this.channelConnections.get(channel)) {
  112. console.warn(`TCP Channel ${channel}未连接,无法发送消息`);
  113. return false;
  114. }
  115. // 如果message是对象,转换为JSON字符串
  116. let messageStr = message;
  117. if (typeof message === 'object') {
  118. messageStr = JSON.stringify(message) + '\n';
  119. }
  120. const sendConfig = {
  121. channel: config.channel || '1', // 注意:channel应该是字符串
  122. message: messageStr
  123. };
  124. // 如果指定了字符编码,添加到配置中
  125. if (config.charsetname) {
  126. sendConfig.charsetname = config.charsetname;
  127. }
  128. TCPSocket.send(sendConfig);
  129. console.log('js成功发送TCP消息:', messageStr);
  130. return true;
  131. }
  132. /**
  133. * TCP断开连接
  134. * @param {Object} config - 断开配置 {channel}
  135. */
  136. disconnect(config = {}) {
  137. const channel = config.channel || TCP_CONFIG.channel;
  138. const disconnectConfig = {
  139. channel: channel
  140. };
  141. TCPSocket.disconnect(disconnectConfig);
  142. this.channelConnections.set(channel, false);
  143. console.log(`TCP连接已断开 - Channel ${channel}`, disconnectConfig);
  144. }
  145. /**
  146. * 添加连接状态监听器
  147. * @param {Function} callback - 回调函数 (status, result) => {}
  148. */
  149. onConnectionChange(callback) {
  150. if (typeof callback === 'function') {
  151. this.connectionCallbacks.push(callback);
  152. }
  153. }
  154. /**
  155. * 添加消息监听器
  156. * @param {Function} callback - 回调函数 (type, message, parsedArray) => {}
  157. */
  158. onMessage(callback) {
  159. if (typeof callback === 'function') {
  160. this.messageCallbacks.push(callback);
  161. }
  162. }
  163. /**
  164. * 移除连接状态监听器
  165. * @param {Function} callback - 要移除的回调函数
  166. */
  167. removeConnectionListener(callback) {
  168. const index = this.connectionCallbacks.indexOf(callback);
  169. if (index > -1) {
  170. this.connectionCallbacks.splice(index, 1);
  171. }
  172. }
  173. /**
  174. * 移除消息监听器
  175. * @param {Function} callback - 要移除的回调函数
  176. */
  177. removeMessageListener(callback) {
  178. const index = this.messageCallbacks.indexOf(callback);
  179. if (index > -1) {
  180. this.messageCallbacks.splice(index, 1);
  181. }
  182. }
  183. /**
  184. * 获取连接状态
  185. * @param {String} channel - 要检查的channel如果不指定则返回所有channel的连接状态
  186. * @returns {Boolean|Object} 连接状态
  187. */
  188. getConnectionStatus(channel = null) {
  189. if (channel) {
  190. return this.channelConnections.get(channel) || false;
  191. }
  192. // 返回所有channel的连接状态
  193. const allConnections = {};
  194. for (const [ch, status] of this.channelConnections) {
  195. allConnections[ch] = status;
  196. }
  197. return allConnections;
  198. }
  199. /**
  200. * 通知连接状态回调
  201. * @private
  202. */
  203. _notifyConnectionCallbacks(status, result, channel) {
  204. this.connectionCallbacks.forEach(callback => {
  205. try {
  206. callback(status, result, channel);
  207. } catch (error) {
  208. console.error('连接状态回调执行错误:', error);
  209. }
  210. });
  211. }
  212. /**
  213. * 通知消息回调
  214. * @private
  215. */
  216. _notifyMessageCallbacks(type, message, parsedArray = null, channel = null) {
  217. this.messageCallbacks.forEach(callback => {
  218. try {
  219. callback(type, message, parsedArray, channel);
  220. } catch (error) {
  221. console.error('消息回调执行错误:', error);
  222. }
  223. });
  224. }
  225. }
  226. // 创建TCP连接实例
  227. const tcpConnection = new TCPConnection();
  228. // 导出TCP连接实例和类
  229. export default tcpConnection;
  230. export { TCPConnection, TCP_CONFIG };