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


Scala IMqttDeliveryToken类代码示例

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


在下文中一共展示了IMqttDeliveryToken类的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)
} 
开发者ID:vavravl1,项目名称:home_center,代码行数:31,代码来源:MqttDispatchingListener.scala

示例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)
  }
} 
开发者ID:camilosampedro,项目名称:lights-on,代码行数:36,代码来源:SubscribeService.scala


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