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.

258 lines
7.5 KiB

4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
  1. /**
  2. * TCP连接工具类
  3. * 用于处理TCP连接发送消息和断开连接
  4. *
  5. * @format
  6. */
  7. // 引用TCP插件
  8. const TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin');
  9. // const TCPSocket = uni.requireNativePlugin("Aimer-TCPPlugin");
  10. // TCP连接配置
  11. const TCP_CONFIG = {
  12. ip: "39.102.136.61",
  13. port: "8088",
  14. channel: "1", // 可选 1~20
  15. charsetname: "UTF-8", // 默认UTF-8,可选GBK
  16. };
  17. /**
  18. * TCP连接管理类
  19. */
  20. class TCPConnection {
  21. constructor() {
  22. this.channelConnections = new Map(); // 存储每个channel的连接状态
  23. this.connectionCallbacks = [];
  24. this.messageCallbacks = [];
  25. }
  26. /**
  27. * TCP初始化连接
  28. * @param {Object} config - 连接配置 {ip, port, channel, charsetname}
  29. * @param {Function} callback - 连接状态回调函数
  30. */
  31. connect(config = {}, callback = null) {
  32. const channel = config.channel || TCP_CONFIG.channel;
  33. // 如果该channel已经连接,先断开现有连接
  34. if (this.channelConnections.get(channel)) {
  35. console.log(`检测到channel ${channel}现有TCP连接,先断开...`);
  36. this.disconnect(config);
  37. // 等待断开完成后再连接
  38. setTimeout(() => {
  39. this._performConnect(config, callback);
  40. }, 300);
  41. } else {
  42. // 直接连接
  43. this._performConnect(config, callback);
  44. }
  45. }
  46. /**
  47. * 执行TCP连接
  48. * @param {Object} config - 连接配置
  49. * @param {Function} callback - 连接状态回调函数
  50. */
  51. _performConnect(config = {}, callback = null) {
  52. const connectionConfig = {
  53. channel: config.channel || TCP_CONFIG.channel,
  54. ip: config.ip || TCP_CONFIG.ip,
  55. port: config.port || TCP_CONFIG.port,
  56. };
  57. // 如果指定了字符集,添加到配置中
  58. if (config.charsetname || TCP_CONFIG.charsetname) {
  59. connectionConfig.charsetname = config.charsetname || TCP_CONFIG.charsetname;
  60. }
  61. console.log("开始建立TCP连接:", connectionConfig);
  62. TCPSocket.connect(connectionConfig, (result) => {
  63. /**
  64. * status : 0 连接成功
  65. * status : 1 断开连接
  66. * receivedMsg : 服务器返回字符串(普通的字符串交互)
  67. * receivedHexMsg : 服务器返回字节数组(单片机智能家居等硬件数据交互)
  68. */
  69. if (result.status == "0") {
  70. // TCP连接成功
  71. this.channelConnections.set(connectionConfig.channel, true);
  72. console.log(`TCP连接成功 - Channel ${connectionConfig.channel}`);
  73. this._notifyConnectionCallbacks("connected", result, connectionConfig.channel);
  74. } else if (result.status == "1") {
  75. // TCP断开连接
  76. this.channelConnections.set(connectionConfig.channel, false);
  77. console.log(`TCP断开连接 - Channel ${connectionConfig.channel}`);
  78. this._notifyConnectionCallbacks("disconnected", result, connectionConfig.channel);
  79. }
  80. if (result.receivedMsg) {
  81. // 服务器返回字符串
  82. console.log("收到字符串消息:", result.receivedMsg);
  83. this._notifyMessageCallbacks("string", result.receivedMsg, null, connectionConfig.channel);
  84. }
  85. // if (result.receivedHexMsg) {
  86. // // 硬件服务器返回16进制数据
  87. // console.log('收到16进制消息:', result.receivedHexMsg);
  88. // let msg = result.receivedHexMsg;
  89. // let sum = msg.length / 2;
  90. // let arr = [];
  91. // for (let k = 0; k < sum; k++) {
  92. // let i = msg.substring(k * 2, k * 2 + 2);
  93. // arr.push(i);
  94. // }
  95. // console.log('解析后的16进制数组:', arr);
  96. // this._notifyMessageCallbacks('hex', result.receivedHexMsg, arr);
  97. // }
  98. // 执行回调函数
  99. if (callback && typeof callback === "function") {
  100. callback(result);
  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 };