-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b90c952
commit 7136bc0
Showing
4 changed files
with
98 additions
and
178 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/client/Mqtt5ClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.client; | ||
|
||
import net.dreamlu.iot.mqtt.codec.MqttPublishMessage; | ||
import net.dreamlu.iot.mqtt.codec.MqttQoS; | ||
import net.dreamlu.iot.mqtt.codec.MqttVersion; | ||
import net.dreamlu.iot.mqtt.core.client.IMqttClientMessageListener; | ||
import net.dreamlu.iot.mqtt.core.client.MqttClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.tio.core.ChannelContext; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
/** | ||
* 客户端测试 | ||
* | ||
* @author L.cm | ||
*/ | ||
public class Mqtt5ClientTest { | ||
private static final Logger logger = LoggerFactory.getLogger(Mqtt5ClientTest.class); | ||
|
||
public static void main(String[] args) { | ||
// 初始化 mqtt 客户端 | ||
MqttClient client = MqttClient.create() | ||
.ip("127.0.0.1") | ||
.port(1883) | ||
.username("mica") | ||
.password("mica") | ||
.version(MqttVersion.MQTT_5) | ||
.cleanSession(false) | ||
.sessionExpiryIntervalSecs(7200) | ||
.connectListener(new MqttClientConnectListener()) | ||
.willMessage(builder -> { | ||
builder.topic("/test/offline") | ||
.messageText("down") | ||
.retain(false) | ||
.qos(MqttQoS.AT_MOST_ONCE); // 遗嘱消息 | ||
}) | ||
// 同步连接,也可以使用 connect() 异步(可以避免 broker 没启动照成启动卡住),但是下面的订阅和发布可能还没连接成功。 | ||
.connectSync(); | ||
|
||
client.subQos0("/test/123", new IMqttClientMessageListener() { | ||
@Override | ||
public void onSubscribed(ChannelContext context, String topicFilter, MqttQoS mqttQoS) { | ||
// 订阅成功之后触发,可在此处做一些业务逻辑 | ||
logger.info("topicFilter:{} MqttQoS:{} 订阅成功!!!", topicFilter, mqttQoS); | ||
} | ||
|
||
@Override | ||
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) { | ||
logger.info(topic + '\t' + new String(payload, StandardCharsets.UTF_8)); | ||
} | ||
}); | ||
|
||
client.publish("/test/client", "mica最牛皮1".getBytes(StandardCharsets.UTF_8)); | ||
client.publish("/test/client", "mica最牛皮2".getBytes(StandardCharsets.UTF_8)); | ||
client.publish("/test/client", "mica最牛皮3".getBytes(StandardCharsets.UTF_8)); | ||
|
||
Timer timer = new Timer(); | ||
timer.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
client.publish("/test/client", "mica最牛皮".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
}, 1000, 2000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 0 additions & 166 deletions
166
mica-mqtt-codec/src/main/java/net/dreamlu/iot/mqtt/codec/WriteBuffer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters