當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript MqttClient.on方法代碼示例

本文整理匯總了TypeScript中mqtt.MqttClient.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript MqttClient.on方法的具體用法?TypeScript MqttClient.on怎麽用?TypeScript MqttClient.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mqtt.MqttClient的用法示例。


在下文中一共展示了MqttClient.on方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: async

          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()
              }
            })
          })
開發者ID:13768324554,項目名稱:chromeless,代碼行數:25,代碼來源:remote.ts

示例2: clearTimeout

          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 },
            )
          })
開發者ID:13768324554,項目名稱:chromeless,代碼行數:14,代碼來源:remote.ts

示例3: 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()
            }
          }
        })
      })
開發者ID:KhaledNobani,項目名稱:chromeless,代碼行數:16,代碼來源:remote.ts

示例4: 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.on方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。