Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
java胎教生
mica-mqtt
提交
ede842be
mica-mqtt
项目概览
java胎教生
/
mica-mqtt
与 Fork 源项目一致
Fork自
mica / mica-mqtt
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica-mqtt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ede842be
编写于
8月 27, 2021
作者:
浅梦2013
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
回退部分代码,保证低版本兼容性。
上级
39bb58ee
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
87 addition
and
36 deletion
+87
-36
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
...ain/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
+47
-0
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerAioListener.java
...t/dreamlu/iot/mqtt/core/server/MqttServerAioListener.java
+3
-3
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerCreator.java
...a/net/dreamlu/iot/mqtt/core/server/MqttServerCreator.java
+0
-7
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/event/IMqttConnectStatusListener.java
...ot/mqtt/core/server/event/IMqttConnectStatusListener.java
+2
-6
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/event/IMqttMessageListener.java
...amlu/iot/mqtt/core/server/event/IMqttMessageListener.java
+1
-3
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/http/api/MqttHttpApi.java
...et/dreamlu/iot/mqtt/core/server/http/api/MqttHttpApi.java
+1
-1
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/support/DefaultMqttConnectStatusListener.java
...core/server/support/DefaultMqttConnectStatusListener.java
+2
-3
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/support/DefaultMqttServerProcessor.java
.../mqtt/core/server/support/DefaultMqttServerProcessor.java
+7
-7
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/biz/Server.java
...xample/src/main/java/net/dreamlu/iot/mqtt/biz/Server.java
+1
-1
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/server/MqttServerTest.java
...main/java/net/dreamlu/iot/mqtt/server/MqttServerTest.java
+1
-1
mica-mqtt-spring-boot-example/src/main/java/net/dreamlu/iot/mqtt/mica/MqttServerTest.java
...c/main/java/net/dreamlu/iot/mqtt/mica/MqttServerTest.java
+1
-1
mica-mqtt-spring-boot-example/src/main/java/net/dreamlu/iot/mqtt/mica/listener/MqttServerMessageListener.java
...mlu/iot/mqtt/mica/listener/MqttServerMessageListener.java
+1
-2
mica-mqtt-spring-boot-starter/src/main/java/net/dreamlu/iot/mqtt/spring/client/MqttClientConfiguration.java
...eamlu/iot/mqtt/spring/client/MqttClientConfiguration.java
+0
-1
mica-mqtt-spring-boot-starter/src/main/java/net/dreamlu/iot/mqtt/spring/server/MqttServerTemplate.java
...et/dreamlu/iot/mqtt/spring/server/MqttServerTemplate.java
+20
-0
未找到文件。
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
浏览文件 @
ede842be
...
...
@@ -72,6 +72,15 @@ public final class MqttServer {
return
this
.
tioServer
;
}
/**
* 获取 http、websocket 服务
*
* @return MqttWebServer
*/
public
MqttWebServer
getWebServer
()
{
return
webServer
;
}
/**
* 获取 ServerTioConfig
*
...
...
@@ -81,6 +90,15 @@ public final class MqttServer {
return
this
.
tioServer
.
getServerTioConfig
();
}
/**
* 获取 mqtt 配置
*
* @return MqttServerCreator
*/
public
MqttServerCreator
getServerCreator
()
{
return
serverCreator
;
}
/**
* 发布消息
*
...
...
@@ -244,6 +262,30 @@ public final class MqttServer {
return
true
;
}
/**
* 获取 ChannelContext
*
* @param clientId clientId
* @return ChannelContext
*/
public
ChannelContext
getChannelContext
(
String
clientId
)
{
return
Tio
.
getByBsId
(
getServerConfig
(),
clientId
);
}
/**
* 服务端主动断开连接
*
* @param clientId clientId
*/
public
void
close
(
String
clientId
)
{
Tio
.
remove
(
getChannelContext
(
clientId
),
"Mqtt server close this connects."
);
}
/**
* 启动服务
*
* @return 是否启动
*/
public
boolean
start
()
{
// 1. 启动 mqtt tcp
try
{
...
...
@@ -262,6 +304,11 @@ public final class MqttServer {
return
true
;
}
/**
* 停止服务
*
* @return 是否停止
*/
public
boolean
stop
()
{
boolean
result
=
this
.
tioServer
.
stop
();
logger
.
info
(
"Mqtt tcp server stop result:{}"
,
result
);
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerAioListener.java
浏览文件 @
ede842be
...
...
@@ -76,7 +76,7 @@ public class MqttServerAioListener extends DefaultAioListener {
// 6. 解绑 clientId
Tio
.
unbindBsId
(
context
);
// 7. 下线事件
notify
(
c
ontext
,
c
lientId
);
notify
(
clientId
);
}
private
void
sendWillMessage
(
ChannelContext
context
,
String
clientId
)
{
...
...
@@ -108,9 +108,9 @@ public class MqttServerAioListener extends DefaultAioListener {
}
}
private
void
notify
(
ChannelContext
context
,
String
clientId
)
{
private
void
notify
(
String
clientId
)
{
try
{
connectStatusListener
.
offline
(
c
ontext
,
c
lientId
);
connectStatusListener
.
offline
(
clientId
);
}
catch
(
Throwable
throwable
)
{
logger
.
error
(
"Mqtt server clientId:{} offline notify error."
,
clientId
,
throwable
);
}
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerCreator.java
浏览文件 @
ede842be
...
...
@@ -31,7 +31,6 @@ import net.dreamlu.iot.mqtt.core.server.support.DefaultMqttConnectStatusListener
import
net.dreamlu.iot.mqtt.core.server.support.DefaultMqttMessageDispatcher
;
import
net.dreamlu.iot.mqtt.core.server.support.DefaultMqttServerAuthHandler
;
import
net.dreamlu.iot.mqtt.core.server.support.DefaultMqttServerProcessor
;
import
net.dreamlu.iot.mqtt.core.server.websocket.MqttWsMsgHandler
;
import
org.tio.core.ssl.SslConfig
;
import
org.tio.core.stat.IpStatListener
;
import
org.tio.server.ServerTioConfig
;
...
...
@@ -40,13 +39,7 @@ import org.tio.server.intf.ServerAioHandler;
import
org.tio.server.intf.ServerAioListener
;
import
org.tio.utils.hutool.StrUtil
;
import
org.tio.utils.thread.pool.DefaultThreadFactory
;
import
org.tio.websocket.common.WsTioUuid
;
import
org.tio.websocket.server.WsServerAioHandler
;
import
org.tio.websocket.server.WsServerAioListener
;
import
org.tio.websocket.server.WsServerConfig
;
import
org.tio.websocket.server.handler.IWsMsgHandler
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Objects
;
import
java.util.concurrent.ScheduledThreadPoolExecutor
;
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/event/IMqttConnectStatusListener.java
浏览文件 @
ede842be
...
...
@@ -16,8 +16,6 @@
package
net.dreamlu.iot.mqtt.core.server.event
;
import
org.tio.core.ChannelContext
;
/**
* mqtt 链接状态事件
*
...
...
@@ -28,17 +26,15 @@ public interface IMqttConnectStatusListener {
/**
* 设备上线(连接成功)
*
* @param context ChannelContext
* @param clientId clientId
*/
void
online
(
ChannelContext
context
,
String
clientId
);
void
online
(
String
clientId
);
/**
* 设备离线
*
* @param context ChannelContext
* @param clientId clientId
*/
void
offline
(
ChannelContext
context
,
String
clientId
);
void
offline
(
String
clientId
);
}
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/event/IMqttMessageListener.java
浏览文件 @
ede842be
...
...
@@ -17,7 +17,6 @@
package
net.dreamlu.iot.mqtt.core.server.event
;
import
net.dreamlu.iot.mqtt.codec.MqttQoS
;
import
org.tio.core.ChannelContext
;
import
java.nio.ByteBuffer
;
...
...
@@ -32,12 +31,11 @@ public interface IMqttMessageListener {
/**
* 监听到消息
*
* @param context ChannelContext
* @param clientId clientId
* @param topic topic
* @param mqttQoS MqttQoS
* @param payload payload
*/
void
onMessage
(
ChannelContext
context
,
String
clientId
,
String
topic
,
MqttQoS
mqttQoS
,
ByteBuffer
payload
);
void
onMessage
(
String
clientId
,
String
topic
,
MqttQoS
mqttQoS
,
ByteBuffer
payload
);
}
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/http/api/MqttHttpApi.java
浏览文件 @
ede842be
...
...
@@ -22,13 +22,13 @@ import net.dreamlu.iot.mqtt.codec.MqttQoS;
import
net.dreamlu.iot.mqtt.core.server.dispatcher.IMqttMessageDispatcher
;
import
net.dreamlu.iot.mqtt.core.server.http.api.code.ResultCode
;
import
net.dreamlu.iot.mqtt.core.server.http.api.form.BaseForm
;
import
net.dreamlu.iot.mqtt.core.util.PayloadEncode
;
import
net.dreamlu.iot.mqtt.core.server.http.api.form.PublishForm
;
import
net.dreamlu.iot.mqtt.core.server.http.api.form.SubscribeForm
;
import
net.dreamlu.iot.mqtt.core.server.http.api.result.Result
;
import
net.dreamlu.iot.mqtt.core.server.http.handler.MqttHttpRoutes
;
import
net.dreamlu.iot.mqtt.core.server.model.Message
;
import
net.dreamlu.iot.mqtt.core.server.session.IMqttSessionManager
;
import
net.dreamlu.iot.mqtt.core.util.PayloadEncode
;
import
org.tio.http.common.HttpRequest
;
import
org.tio.http.common.HttpResponse
;
import
org.tio.http.common.Method
;
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/support/DefaultMqttConnectStatusListener.java
浏览文件 @
ede842be
...
...
@@ -19,7 +19,6 @@ package net.dreamlu.iot.mqtt.core.server.support;
import
net.dreamlu.iot.mqtt.core.server.event.IMqttConnectStatusListener
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.tio.core.ChannelContext
;
/**
* 默认的链接状态监听
...
...
@@ -30,12 +29,12 @@ public class DefaultMqttConnectStatusListener implements IMqttConnectStatusListe
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
DefaultMqttConnectStatusListener
.
class
);
@Override
public
void
online
(
ChannelContext
context
,
String
clientId
)
{
public
void
online
(
String
clientId
)
{
logger
.
info
(
"Mqtt clientId:{} online."
,
clientId
);
}
@Override
public
void
offline
(
ChannelContext
context
,
String
clientId
)
{
public
void
offline
(
String
clientId
)
{
logger
.
info
(
"Mqtt clientId:{} offline."
,
clientId
);
}
}
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/support/DefaultMqttServerProcessor.java
浏览文件 @
ede842be
...
...
@@ -118,7 +118,7 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
// 8. 返回 ack
connAckByReturnCode
(
clientId
,
context
,
MqttConnectReturnCode
.
CONNECTION_ACCEPTED
);
// 9. 在线状态
connectStatusListener
.
online
(
c
ontext
,
c
lientId
);
connectStatusListener
.
online
(
clientId
);
}
private
void
connAckByReturnCode
(
String
clientId
,
ChannelContext
context
,
MqttConnectReturnCode
returnCode
)
{
...
...
@@ -141,10 +141,10 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
logger
.
debug
(
"Publish - clientId:{} topicName:{} mqttQoS:{} packetId:{}"
,
clientId
,
topicName
,
mqttQoS
,
packetId
);
switch
(
mqttQoS
)
{
case
AT_MOST_ONCE:
invokeListenerForPublish
(
c
ontext
,
c
lientId
,
mqttQoS
,
topicName
,
message
,
fixedHeader
.
isRetain
());
invokeListenerForPublish
(
clientId
,
mqttQoS
,
topicName
,
message
,
fixedHeader
.
isRetain
());
break
;
case
AT_LEAST_ONCE:
invokeListenerForPublish
(
c
ontext
,
c
lientId
,
mqttQoS
,
topicName
,
message
,
fixedHeader
.
isRetain
());
invokeListenerForPublish
(
clientId
,
mqttQoS
,
topicName
,
message
,
fixedHeader
.
isRetain
());
if
(
packetId
!=
-
1
)
{
MqttMessage
messageAck
=
MqttMessageBuilders
.
pubAck
()
.
packetId
(
packetId
)
...
...
@@ -215,7 +215,7 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
MqttFixedHeader
incomingFixedHeader
=
incomingPublish
.
fixedHeader
();
MqttQoS
mqttQoS
=
incomingFixedHeader
.
qosLevel
();
boolean
retain
=
incomingFixedHeader
.
isRetain
();
invokeListenerForPublish
(
c
ontext
,
c
lientId
,
mqttQoS
,
topicName
,
incomingPublish
,
retain
);
invokeListenerForPublish
(
clientId
,
mqttQoS
,
topicName
,
incomingPublish
,
retain
);
pendingQos2Publish
.
onPubRelReceived
();
sessionManager
.
removePendingQos2Publish
(
clientId
,
messageId
);
}
...
...
@@ -312,8 +312,8 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
* @param topicName topicName
* @param message MqttPublishMessage
*/
private
void
invokeListenerForPublish
(
ChannelContext
context
,
String
clientId
,
MqttQoS
mqttQoS
,
String
topicName
,
MqttPublishMessage
message
,
boolean
isRetain
)
{
private
void
invokeListenerForPublish
(
String
clientId
,
MqttQoS
mqttQoS
,
String
topicName
,
MqttPublishMessage
message
,
boolean
isRetain
)
{
ByteBuffer
payload
=
message
.
payload
();
// 1. retain 消息逻辑
if
(
isRetain
)
{
...
...
@@ -331,7 +331,7 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
}
}
// 2. 消息发布
messageListener
.
onMessage
(
c
ontext
,
c
lientId
,
topicName
,
mqttQoS
,
payload
);
messageListener
.
onMessage
(
clientId
,
topicName
,
mqttQoS
,
payload
);
}
}
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/biz/Server.java
浏览文件 @
ede842be
...
...
@@ -40,7 +40,7 @@ public class Server {
// 1. 消息转发处理器,可用来实现集群
IMqttMessageDispatcher
messageDispatcher
=
new
DefaultMqttMessageDispatcher
();
// 2. 收到消息,将消息转发出去
IMqttMessageListener
messageListener
=
(
c
ontext
,
c
lientId
,
topic
,
mqttQoS
,
payload
)
->
{
IMqttMessageListener
messageListener
=
(
clientId
,
topic
,
mqttQoS
,
payload
)
->
{
Message
message
=
new
Message
();
message
.
setTopic
(
topic
);
message
.
setQos
(
mqttQoS
.
value
());
...
...
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/server/MqttServerTest.java
浏览文件 @
ede842be
...
...
@@ -46,7 +46,7 @@ public class MqttServerTest {
// .maxBytesInMessage(1024 * 100)
// mqtt 3.1 协议会校验 clientId 长度。
// .maxClientIdLength(64)
.
messageListener
((
c
ontext
,
c
lientId
,
topic
,
mqttQoS
,
payload
)
->
{
.
messageListener
((
clientId
,
topic
,
mqttQoS
,
payload
)
->
{
logger
.
info
(
"clientId:{} topic:{} mqttQoS:{} message:{}"
,
clientId
,
topic
,
mqttQoS
,
ByteBufferUtil
.
toString
(
payload
));
})
.
debug
()
// 开启 debug 信息日志
...
...
mica-mqtt-spring-boot-example/src/main/java/net/dreamlu/iot/mqtt/mica/MqttServerTest.java
浏览文件 @
ede842be
...
...
@@ -45,7 +45,7 @@ public class MqttServerTest {
.
readBufferSize
(
512
)
// 关闭 websocket,避免和 spring boot 启动的冲突
.
websocketEnable
(
false
)
.
messageListener
((
c
ontext
,
c
lientId
,
topic
,
mqttQoS
,
payload
)
->
{
.
messageListener
((
clientId
,
topic
,
mqttQoS
,
payload
)
->
{
logger
.
info
(
"clientId:{} topic:{} mqttQoS:{} message:{}"
,
clientId
,
topic
,
mqttQoS
,
ByteBufferUtil
.
toString
(
payload
));
})
.
debug
()
// 开启 debug 信息日志
...
...
mica-mqtt-spring-boot-example/src/main/java/net/dreamlu/iot/mqtt/mica/listener/MqttServerMessageListener.java
浏览文件 @
ede842be
...
...
@@ -6,7 +6,6 @@ import net.dreamlu.iot.mqtt.core.server.event.IMqttMessageListener;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Service
;
import
org.tio.core.ChannelContext
;
import
java.nio.ByteBuffer
;
...
...
@@ -18,7 +17,7 @@ public class MqttServerMessageListener implements IMqttMessageListener {
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
MqttServerMessageListener
.
class
);
@Override
public
void
onMessage
(
ChannelContext
context
,
String
clientId
,
String
topic
,
MqttQoS
mqttQoS
,
ByteBuffer
byteBuffer
)
{
public
void
onMessage
(
String
clientId
,
String
topic
,
MqttQoS
mqttQoS
,
ByteBuffer
byteBuffer
)
{
logger
.
info
(
"clientId:{} topic:{} mqttQoS:{} message:{}"
,
clientId
,
topic
,
mqttQoS
,
ByteBufferUtil
.
toString
(
byteBuffer
));
}
}
mica-mqtt-spring-boot-starter/src/main/java/net/dreamlu/iot/mqtt/spring/client/MqttClientConfiguration.java
浏览文件 @
ede842be
...
...
@@ -19,7 +19,6 @@ package net.dreamlu.iot.mqtt.spring.client;
import
net.dreamlu.iot.mqtt.core.client.IMqttClientConnectListener
;
import
net.dreamlu.iot.mqtt.core.client.MqttClient
;
import
net.dreamlu.iot.mqtt.core.client.MqttClientCreator
;
import
net.dreamlu.iot.mqtt.core.client.MqttWillMessage
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
...
...
mica-mqtt-spring-boot-starter/src/main/java/net/dreamlu/iot/mqtt/spring/server/MqttServerTemplate.java
浏览文件 @
ede842be
...
...
@@ -145,4 +145,24 @@ public class MqttServerTemplate {
public
boolean
publishAll
(
String
topic
,
ByteBuffer
payload
,
MqttQoS
qos
,
boolean
retain
)
{
return
mqttServer
.
publishAll
(
topic
,
payload
,
qos
,
retain
);
}
/**
* 获取 ChannelContext
*
* @param clientId clientId
* @return ChannelContext
*/
public
ChannelContext
getChannelContext
(
String
clientId
)
{
return
mqttServer
.
getChannelContext
(
clientId
);
}
/**
* 服务端主动断开连接
*
* @param clientId clientId
*/
public
void
close
(
String
clientId
)
{
mqttServer
.
close
(
clientId
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录