サンプルコード(受信)
@GrabResolver(name='paho-rel', root='https://repo.eclipse.org/content/repositories/paho-releases/') @Grab(group='org.eclipse.paho', module='mqtt-client', version='0.4.0') import org.eclipse.paho.client.mqttv3.* import org.eclipse.paho.client.mqttv3.persist.* def host = "192.168.1.219" // replace this def broker = "tcp://${host}:1883" def clientId = "groovySubscriber" def persistence = new MemoryPersistence() def client = new MqttClient(broker, clientId, persistence) def opts = new MqttConnectOptions() //opts.setKeepAliveInterval(30) opts.setCleanSession(true) client.connect(opts) client.setCallback(new MqttCallback(){ void connectionLost(Throwable cause){ println "connection lost." println cause cause.printStackTrace() System.exit(1) } void deliveryComplete(IMqttDeliveryToken token){ println "completed delivery." } void messageArrived(String topic, MqttMessage message){ println "received: ${new String(message.getPayload(), "UTF-8")}" } }) client.subscribe("topic/test") System.console().readLine 'please press enter\n' client.disconnect()サンプルコード(送信)
@GrabResolver(name='paho-rel', root='https://repo.eclipse.org/content/repositories/paho-releases/') @Grab(group='org.eclipse.paho', module='mqtt-client', version='0.4.0') import org.eclipse.paho.client.mqttv3.* import org.eclipse.paho.client.mqttv3.persist.* def host = "192.168.1.219" // replace this def broker = "tcp://${host}:1883" def clientId = "groovyPublisher" def persistence = new MemoryPersistence() def client = new MqttClient(broker, clientId, persistence) def opts = new MqttConnectOptions() //opts.setKeepAliveInterval(30) opts.setCleanSession(true) client.connect(opts) def message = new MqttMessage("サンプル".getBytes("UTF-8")) message.setQos(0) def topic = client.getTopic("topic/test") def token = topic.publish(message) token.waitForCompletion() client.disconnect()動作環境
groovy 2.3.6, JDK7 update 65, RabbitMQ 3.3.5
0 件のコメント:
コメントを投稿