当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript mqtt.MqttClient类代码示例

本文整理汇总了TypeScript中mqtt.MqttClient的典型用法代码示例。如果您正苦于以下问题:TypeScript MqttClient类的具体用法?TypeScript MqttClient怎么用?TypeScript MqttClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了MqttClient类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: reject

    const promise = new Promise<T>((resolve, reject) => {
      this.channel.subscribe(this.TOPIC_RESPONSE, () => {
        this.channel.on('message', (topic, buffer) => {
          if (this.TOPIC_RESPONSE === topic) {
            const message = buffer.toString()
            const result = JSON.parse(message) as RemoteResult

            if (result.error) {
              reject(result.error)
            } else if (result.value) {
              resolve(result.value)
            } else {
              resolve()
            }
          }
        })
        this.channel.publish(this.TOPIC_REQUEST, JSON.stringify(command))
      })
    })
开发者ID:d2s,项目名称:chromeless,代码行数:19,代码来源:remote.ts

示例2: Promise

    await new Promise(async (resolve, reject) => {
      const timeout = setTimeout(() => {
        if (this.channel) {
          this.channel.end()
        }

        reject(
          new Error(
            "Timed out after 30sec. Connection couldn't be established.",
          ),
        )
      }, 30000)

      try {
        const { endpointUrl, apiKey } = getEndpoint(this.options.remote)
        const { body: { url, channelId } } = await got(endpointUrl, {
          headers: apiKey
            ? {
                'x-api-key': apiKey,
              }
            : undefined,
          json: true,
        })

        this.channelId = channelId

        this.TOPIC_NEW_SESSION = 'chrome/new-session'
        this.TOPIC_CONNECTED = `chrome/${channelId}/connected`
        this.TOPIC_REQUEST = `chrome/${channelId}/request`
        this.TOPIC_RESPONSE = `chrome/${channelId}/response`
        this.TOPIC_END = `chrome/${channelId}/end`

        const channel = mqtt(url, {
          will: {
            topic: 'chrome/last-will',
            payload: JSON.stringify({ channelId }),
            qos: 1,
            retain: false,
          },
        })

        this.channel = channel

        if (this.options.debug) {
          channel.on('error', error => console.log('WebSocket error', error))
          channel.on('offline', () => console.log('WebSocket offline'))
        }

        channel.on('connect', () => {
          if (this.options.debug) {
            console.log('Connected to message broker.')
          }

          channel.subscribe(this.TOPIC_CONNECTED, { qos: 1 }, () => {
            channel.on('message', async topic => {
              if (this.TOPIC_CONNECTED === topic) {
                clearTimeout(timeout)
                resolve()
              }
            })

            channel.publish(
              this.TOPIC_NEW_SESSION,
              JSON.stringify({ channelId, options: this.options }),
              { qos: 1 },
            )
          })

          channel.subscribe(this.TOPIC_END, () => {
            channel.on('message', async (topic, buffer) => {
              if (this.TOPIC_END === topic) {
                const message = buffer.toString()
                const data = JSON.parse(message)

                if (data.outOfTime) {
                  console.warn(
                    `Chromeless Proxy disconnected because it reached it's execution time limit (5 minutes).`,
                  )
                } else if (data.inactivity) {
                  console.warn(
                    'Chromeless Proxy disconnected due to inactivity (no commands sent for 30 seconds).',
                  )
                } else {
                  console.warn(
                    `Chromeless Proxy disconnected (we don't know why).`,
                    data,
                  )
                }

                await this.close()
              }
            })
          })
        })
      } catch (error) {
        console.error(error)

        reject(
          new Error('Unable to get presigned websocket URL and connect to it.'),
        )
//.........这里部分代码省略.........
开发者ID:13768324554,项目名称:chromeless,代码行数:101,代码来源:remote.ts


注:本文中的mqtt.MqttClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。