Skip to content

Commit

Permalink
✨ mica-mqtt-server 用户绑定使用 tio 内置
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed May 20, 2024
1 parent 3b3bc80 commit 4624f41
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ public interface MqttConst {
* 是 http 协议
*/
String IS_HTTP = "is_http";
/**
* 用户名 key
*/
String USER_NAME_KEY = "username";
/**
* session 有效期,小于等于 0,关闭时清理,大于 0 采用缓存处理
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ public void onBeforeClose(ChannelContext context, Throwable throwable, String re
// 6. 会话清理
cleanSession(clientId);
// 7. 下线事件
String username = context.get(MqttConst.USER_NAME_KEY);
context.remove(MqttConst.USER_NAME_KEY);
notify(context, clientId, username, remark);
notify(context, clientId, remark);
}

private void sendWillMessage(String clientId) {
Expand All @@ -133,7 +131,8 @@ private void cleanSession(String clientId) {
}
}

private void notify(ChannelContext context, String clientId, String username, String remark) {
private void notify(ChannelContext context, String clientId, String remark) {
String username = context.getUserId();
executor.execute(() -> {
try {
connectStatusListener.offline(context, clientId, username, remark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import net.dreamlu.iot.mqtt.codec.*;
import net.dreamlu.iot.mqtt.core.common.MqttPendingPublish;
import net.dreamlu.iot.mqtt.core.common.MqttPendingQos2Publish;
import net.dreamlu.iot.mqtt.core.server.MqttConst;
import net.dreamlu.iot.mqtt.core.server.MqttServerCreator;
import net.dreamlu.iot.mqtt.core.server.MqttServerProcessor;
import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerAuthHandler;
Expand Down Expand Up @@ -130,7 +129,7 @@ public void processConnect(ChannelContext context, MqttConnectMessage mqttMessag
// 5. 绑定 uniqueId、保存 username
Tio.bindBsId(context, uniqueId);
if (StrUtil.isNotBlank(userName)) {
context.set(MqttConst.USER_NAME_KEY, userName);
Tio.bindUser(context, userName);
}
// 6. 心跳超时时间,当然这个值如果小于全局配置(默认:120s),定时检查的时间间隔还是以全局为准,只是在判断时用此值
float keepAliveBackoff = serverCreator.getKeepaliveBackoff();
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<!-- mica-mqtt version -->
<revision>2.2.14-SNAPSHOT</revision>
<revision>2.3.0-SNAPSHOT</revision>
<!-- java version -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down

0 comments on commit 4624f41

Please sign in to comment.