-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ mica-mqtt-client 添加连接测试功能 connectTest
- Loading branch information
1 parent
71485e9
commit b22b97c
Showing
4 changed files
with
283 additions
and
2 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/client/MqttClientConnTest.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,40 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & 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.MqttConnectReasonCode; | ||
import net.dreamlu.iot.mqtt.core.client.MqttClient; | ||
|
||
/** | ||
* 客户端测试 | ||
* | ||
* @author L.cm | ||
*/ | ||
public class MqttClientConnTest { | ||
|
||
public static void main(String[] args) throws Exception { | ||
// 初始化 mqtt 客户端 | ||
MqttConnectReasonCode reasonCode = MqttClient.create() | ||
// .ip("127.0.0.1") | ||
.ip("mqtt.dreamlu.net") | ||
.port(1883) | ||
.username("mica") | ||
.password("mica1") | ||
.connectTest(); | ||
System.out.println(reasonCode); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...client/src/main/java/net/dreamlu/iot/mqtt/core/client/MqttClientConnectTestProcessor.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,88 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & 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.core.client; | ||
|
||
import net.dreamlu.iot.mqtt.codec.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.tio.core.ChannelContext; | ||
import org.tio.core.Tio; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* 默认的 mqtt 消息处理器 | ||
* | ||
* @author L.cm | ||
*/ | ||
public class MqttClientConnectTestProcessor implements IMqttClientProcessor { | ||
private static final Logger logger = LoggerFactory.getLogger(MqttClientConnectTestProcessor.class); | ||
private final CompletableFuture<MqttConnectReasonCode> future; | ||
|
||
public MqttClientConnectTestProcessor(CompletableFuture<MqttConnectReasonCode> future) { | ||
this.future = future; | ||
} | ||
|
||
@Override | ||
public void processDecodeFailure(ChannelContext context, MqttMessage message, Throwable ex) { | ||
// 客户端失败,默认记录异常日志 | ||
logger.error(ex.getMessage(), ex); | ||
} | ||
|
||
@Override | ||
public void processConAck(ChannelContext context, MqttConnAckMessage message) { | ||
MqttConnAckVariableHeader connAckVariableHeader = message.variableHeader(); | ||
Tio.remove(context, "mqtt connect tested."); | ||
future.complete(connAckVariableHeader.connectReturnCode()); | ||
} | ||
|
||
@Override | ||
public void processSubAck(ChannelContext context, MqttSubAckMessage message) { | ||
|
||
} | ||
|
||
@Override | ||
public void processPublish(ChannelContext context, MqttPublishMessage message) { | ||
|
||
} | ||
|
||
@Override | ||
public void processUnSubAck(MqttUnsubAckMessage message) { | ||
|
||
} | ||
|
||
@Override | ||
public void processPubAck(MqttPubAckMessage message) { | ||
|
||
} | ||
|
||
@Override | ||
public void processPubRec(ChannelContext context, MqttMessage message) { | ||
|
||
} | ||
|
||
@Override | ||
public void processPubRel(ChannelContext context, MqttMessage message) { | ||
|
||
} | ||
|
||
@Override | ||
public void processPubComp(MqttMessage message) { | ||
|
||
} | ||
|
||
} |
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
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