本文整理匯總了TypeScript中mqtt.MqttClient.subscribe方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript MqttClient.subscribe方法的具體用法?TypeScript MqttClient.subscribe怎麽用?TypeScript MqttClient.subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mqtt.MqttClient
的用法示例。
在下文中一共展示了MqttClient.subscribe方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: clearTimeout
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()
}
})
})
})
示例2: 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()
}
}
})
})
})