更新记录

1.0.0(2024-10-11)

1.0.0

第一个版本


平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.7.13,Android:4.4,iOS:9,HarmonyOS:支持 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

wtto-TCPSocket

引入插件

import * as tcpSocket from '@/uni_modules/wtto-TCPSocket'

类型定义以及说明

declare module '@/uni_modules/wtto-TCPSocket' {
  /** 错误回调的参数 */
  interface ErrorCallbackOptions {
    /** 错误信息 */
    errMsg: string
  }

  /**
   * 连接TCP Socket
   * @param options tcp地址
   * @param connectCallback 连接成功后的回调
   * @param errorCallback 发生错误的回调,全生命周期有效,即其他接口的错误信息也是使用此回调
   * @example
   * ```js
   * const TCP_ADDRESS = "192.168.31.254";
   * const TCP_PORT = 6668;
   * tcpSocket.connect(
   *   {
   *     address: TCP_ADDRESS,
   *     port: TCP_PORT,
   *   },
   *   () => {
   *     console.log("连接成功");
   *     // do your actions
   *   },
   *   (err) => {
   *     console.error(`出错了: ${err.errMsg}`);
   *   }
   * );
   * ```
   */
  function connect(
    options: { address: string; port: number },
    connectCallback: () => void,
    errorCallback: (err: ErrorCallbackOptions) => void
  ): void

  /**
   * 主动关闭连接
   * @example
   * ```js
   * tcpSocket.close();
   * ```
   */
  function close(): void

  /**
   * 发送buffer消息
   * @param data buffer转为整型数组
   * @example
   * ```js
   * // 72=0x48='H' 105=0x69='i'
   * const buffer = new Uint8Array([72, 105]);
   * tcpSocket.write([...buffer]);
   * ```
   */
  function write(data: number[]): void

  /**
   * 发送字符串消息
   * @param data 发送的内容
   * @example
   * ```js
   * tcpSocket.writeString("Hi");
   * ```
   */
  function writeString(data: string): void

  /**
   * 监听接收消息
   * @param messageCallback 接收消息回调
   * @example
   * ```js
   * tcpSocket.onMessage((data) => {
   *   console.log(`接收到消息: ${JSON.stringify(data)}`);
   *   // number[] => buffer
   *   const buffer = new Uint8Array(data);
   *   // buffer => string
   *   const message = buffer.reduce(
   *     (msg, item) => msg + String.fromCharCode(item),
   *     ""
   *   );
   *   console.log(`接收到消息转为字符串: ${message}`);
   * });
   * ```
   */
  function onMessage(messageCallback: (data: number[]) => void): void

  /**
   * 监听连接关闭
   * @param closeCallback 连接关闭回调
   */
  function onClose(closeCallback: () => void): void
}

API 使用

连接 TCP Socket

const TCP_ADDRESS = '192.168.31.254'
const TCP_PORT = 6668
tcpSocket.connect(
  {
    address: TCP_ADDRESS,
    port: TCP_PORT,
  },
  () => {
    // 连接成功后的回调
    console.log('连接成功')
    // do your actions
    // 调用onClose和onMessage方法,监听回调
    // 之后可以调用write方法,发送消息
  },
  (err) => {
    // 发生错误的回调,全生命周期有效,即其他接口的错误信息也是使用此回调
    console.error(`出错了: ${err.errMsg}`)
  }
)

主动断开连接

tcpSocket.close()

监听连接断开事件

tcpSocket.onClose(() => {
  console.log('已断开连接')
})

监听接收消息

/** @param {{message:number[]}} data */
tcpSocket.onMessage((data) => {
  console.log(`接收到消息: ${JSON.stringify(data)}`)
  const buffer = new Uint8Array(data)
  // buffer => string
  const message = buffer.reduce((msg, item) => msg + String.fromCharCode(item), '')
  console.log(`接收到消息转为字符串: ${message}`)
})

发送字符串消息

tcpSocket.writeString('Hi')

发送 Buffer 消息

// 72=0x48='H' 105=0x69='i'
const buffer = new Uint8Array([72, 105])
tcpSocket.write([...buffer])

隐私、权限声明

1. 本插件需要申请的系统权限列表:

Android: android.permission.INTERNET; Harmony: ohos.permission.INTERNET; iOS: 无

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问