From 7c77f933812d470238c87ecf25613e1f47bb60b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Tue, 16 Apr 2024 09:54:04 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/mqtt/proxy/MqttClientProxy.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/proxy/MqttClientProxy.java diff --git a/example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/proxy/MqttClientProxy.java b/example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/proxy/MqttClientProxy.java new file mode 100644 index 00000000..a92b016a --- /dev/null +++ b/example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/proxy/MqttClientProxy.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.proxy; + +import net.dreamlu.iot.mqtt.codec.MqttQoS; +import net.dreamlu.iot.mqtt.core.client.MqttClient; + +/** + * 2 个 mqtt 服务间,使用 2 个 client 做数据传输 + * + * @author L.cm + */ +public class MqttClientProxy { + + public static void main(String[] args) { + MqttClient client1 = MqttClient.create() + .ip("ip1") + .port(1883) + .clientId("clientI") + .username("admin&admin") + .password("6b408b34171b0423160dcb8b70decefe") + .debug() + .connectSync(); + + MqttClient client2 = MqttClient.create() + .ip("ip2") + .port(1883) + .clientId("client2") + .username("admin&admin") + .password("6b408b34171b0423160dcb8b70decefe") + .debug() + .connectSync(); + + String[] topics = new String[]{ + "$share/test/link/product1/+/event/+/post", + "$share/test/link/product2/+/event/+/post", + "$share/test/link/product3/+/event/+/post" + }; + client1.subscribe(topics, MqttQoS.AT_MOST_ONCE, (context, topic, message, payload) -> { + client2.publish(topic, payload); + }); + } + +}