本文整理汇总了Scala中org.eclipse.paho.client.mqttv3.MqttCallback类的典型用法代码示例。如果您正苦于以下问题:Scala MqttCallback类的具体用法?Scala MqttCallback怎么用?Scala MqttCallback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MqttCallback类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MqttDispatchingListener
//设置package包名称以及导入依赖的类
package mqtt
import akka.actor.{ActorPath, ActorSystem}
import mqtt.MqttListenerMessage.ConsumeMessage
import org.eclipse.paho.client.mqttv3.{IMqttDeliveryToken, MqttCallback, MqttMessage}
import play.api.Logger
class MqttDispatchingListener(actorSystem: ActorSystem) extends MqttCallback {
var listeners: List[ActorPath] = List.empty
override def deliveryComplete(token: IMqttDeliveryToken): Unit = {}
override def connectionLost(cause: Throwable): Unit = {}
def addListener(listener:ActorPath): Unit = {
listeners = listener :: listeners
}
override def messageArrived(topic: String, message: MqttMessage): Unit = {
val msgStr = new String(message.getPayload)
Logger.debug(s"Mqtt topic $topic received $msgStr")
listeners.foreach(actorSystem.actorSelection(_) ! ConsumeMessage(topic, msgStr))
}
}
object MqttListenerMessage {
case class Ping()
case class ConsumeMessage(receivedTopic: String, message: String)
}
示例2: SubscribeService
//设置package包名称以及导入依赖的类
package service
import controllers.socket.LightSocketActor
import model.Message
import org.eclipse.paho.client.mqttv3.{IMqttDeliveryToken, MqttCallback, MqttClient, MqttMessage}
object SubscribeService {
val client = new MqttClient(brokerUrl, MqttClient.generateClientId(), persistence)
def subscribe(): Unit = {
client.connect()
// TODO: Change topic
client.subscribe(topic)
val callback = new MqttCallback {
override def deliveryComplete(token: IMqttDeliveryToken): Unit = {
play.Logger.debug("Delivery complete!")
}
override def connectionLost(cause: Throwable): Unit = {
play.Logger.debug("Connection to socket lost")
LightSocketActor.sendMessage(Message("Connection lost"))
}
override def messageArrived(topic: String, message: MqttMessage): Unit = {
play.Logger.debug(s"A message arrived: ${new String(message.getPayload)}")
LightSocketActor.sendMessage(Message(new String(message.getPayload)))
}
}
client.setCallback(callback)
}
}