TCP插件API文档(Uniapp)
    正在准备搜索索引...

    类 UtsTcpClientManager

    TCP 客户端管理器。封装的TCP客户端的相关操作,包括:创建TCP客户端、断开客户端,发送消息等功能。

    tickstep

    1.0.2

    // 创建一个TCP客户端实例
    let tcpClientManager = new UtsTcpClientManager()
    tcpClientManager.createNewConnection(
    "my-tcp-client",
    {
    serverAddress: "192.168.102.127",
    serverPort: 29999,
    dataType: "text", // 接收的数据格式,支持:text-UTF8字符串,hex-十六进制HEX字符串,byte-二进制字节数组
    }, {
    onConnectError(id, errorStr) {
    console.log(id, "客户端错误:", errorStr)
    },
    onConnected(id) {
    console.log(id, "客户端建立连接")
    },
    onReceivedMessage(message) {
    console.log("客户端接收到消息:", message)
    },
    onDisconnected(id) {
    console.log(id, "客户端断开连接")
    },
    }, errMsg => {
    log("创建TCP链接结果回调:", errMsg)
    })
    索引

    构造函数

    方法

    • 创建TCP客户端并链接到服务器

      参数

      返回 void

      此函数不返回任何值

      tcpClientManager.createNewConnection(
      "my-tcp-client",
      {
      serverAddress: "192.168.102.127",
      serverPort: 29999,
      }, {
      onConnectError(id, errorStr) {
      console.log(id, "客户端错误:", errorStr)
      },
      onConnected(id) {
      console.log(id, "客户端建立连接")
      },
      onReceivedMessage(message) {
      console.log("客户端接收到消息:", message)
      },
      onDisconnected(id) {
      console.log(id, "客户端断开连接")
      },
      }, errMsg => {
      log("创建TCP链接结果回调:", errMsg)
      })
    • 获取指定connectionId的链接信息

      参数

      • connectionId: string

        TCP客户端的唯一标识

      返回 null | UtsTcpClientConnection

      返回链接信息,如果没有则返回null

    • 获取指定connectionId的链接的状态

      参数

      • connectionId: string

        TCP客户端的唯一标识

      返回 boolean

      返回状态信息,true-连接中,false-已断开

    • 通过指定的链接客户端发送消息给服务器

      参数

      返回 void

      此函数不返回任何值

      // 发送文本消息
      tcpClientManager.sendMessage({
      connectionId: "my-tcp-client",
      dataType: "text",
      data: "hello!!!!!!!!"
      }, errMsg => {
      log("发送结果回调:", errMsg)
      })
      // 发送byte字节消息
      tcpClientManager.sendMessage({
      connectionId: "my-tcp-client",
      dataType: "byte",
      byteData: [0x68, 0x65, 0x6c,0x6c,0x6f,0x2c,0x31, 0x32, 0x33],
      }, errMsg => {
      log("发送结果回调:", errMsg)
      })
    • 移除并关闭指定connectionId的客户端链接

      参数

      • connectionId: string

        TCP客户端的唯一标识

      返回 boolean

      返回状态信息,true-关闭成功,false-关闭失败

    • 移除并关闭所有的客户端链接

      返回 void

      此函数不返回任何值