44 lines
1.4 KiB
Java
44 lines
1.4 KiB
Java
package com.mobai.util;
|
|
|
|
import com.mobai.cofig.MqttFactory;
|
|
import com.mobai.cofig.MqttProperties;
|
|
import com.mobai.domian.MqttCallBackServiceImpl;
|
|
import com.mobai.domian.MqttServerModel;
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
/**
|
|
* @ClassName ConnectMqtt
|
|
* @description mqtt连接工具
|
|
* @author Mobai
|
|
* @date 2024/6/5 11:33
|
|
*/
|
|
@Component
|
|
public class ConnectMqtt {
|
|
|
|
@Autowired
|
|
private RedisService redisService;
|
|
|
|
public void connectMq(MqttServerModel mqttServerModel) throws ServletException {
|
|
if (mqttServerModel == null) {
|
|
throw new ServletException("连接参数不能为空");
|
|
}
|
|
MqttProperties mqttProperties = MqttProperties.configBuild(mqttServerModel.getBroker(), mqttServerModel.getTopic());
|
|
// MqttProperties mqttProperties = new MqttProperties();
|
|
// mqttProperties.setBroker("tcp://39.98.69.92:1883");
|
|
// mqttProperties.setTopic("mqtt/test");
|
|
mqttProperties.setUsername("emqx");
|
|
mqttProperties.setPassword("public");
|
|
// mqttProperties.setClientid("subscribe_client");
|
|
int qos = 0;
|
|
try {
|
|
MqttClient client = new MqttFactory(new MqttCallBackServiceImpl()).buildOptions(mqttProperties);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|