我的学生项目需要帮助。 目前我正在使用JavaFX,MQTT和Mysql制作聊天应用程序。 我想创建一个用户列表(哪个用户在线)
我尝试了这个,以便新用户通过特殊主题发送消息,并且所有用户都将" secretely"订阅此主题后,它将接收消息(使用回调)并调用insertMysql()和updatingList()方法。但不知怎的,它不起作用。
这是我提供用户名的地方
public static void logging(String username) throws MqttException {
if(username != null && !username.isEmpty()) {
MysqlDatabase.insertingMysql(username);
window.close();
}
}
这是添加ListView
public static void addItem(String item) {
userList.getItems().add(item);
}
这是我遇到问题的部分(我认为)
public class MysqlDatabase {
static String topic_body = "chat";;
static String topic_ext = "userList";
static MqttCallback sqlCallback = new MqttCallback() {
@Override
public void connectionLost(Throwable thrwbl) {
}
@Override
public void messageArrived(String string, MqttMessage mm) throws Exception {
String username = new String(mm.getPayload());
System.out.println(username); // Checking
updatingList();
}
@Override
public void deliveryComplete(IMqttDeliveryToken imdt) {
}
};
public static void sqlConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Connected");
}
public static void mqttConnection(String username) throws MqttException {
MqttClient sqlClient = new MqttClient("tcp://iot.eclipse.org:1883", username);
sqlClient.connect();
sqlClient.subscribe(topic_body + topic_ext + "#");
sqlClient.setCallback(sqlCallback);
MqttMessage sqlMessage = new MqttMessage();
sqlMessage.setPayload((username).getBytes());
sqlClient.publish(topic_body + "userlist", sqlMessage);
}
public static void insertingMysql(String username) throws MqttException {
sqlConnection();
String host = "jdbc:mysql://localhost/test";
String user = "root";
String password = "";
try {
Connection connect = DriverManager.getConnection(host, user, password);
PreparedStatement statement = (PreparedStatement) connect.prepareStatement("INSERT INTO chat_test(username)VALUES(?)");
statement.setString(1, username);
statement.executeUpdate();
statement.close();
System.out.println("Inserted to database!");
mqttConnection(username);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("Error");
e.printStackTrace();
}
}
public static void updatingList() {
sqlConnection();
String host = "jdbc:mysql://localhost/test";
String user = "root";
String password = "";
try {
Connection connect = DriverManager.getConnection(host, user, password);
PreparedStatement statement = (PreparedStatement) connect.prepareStatement("SELECT * FROM chat_test");
ResultSet rs = statement.executeQuery();
while(rs.next()) {
String uName = rs.getString("username");
System.out.println("Username: " + uName);
JavaFXChat.addItem(uName);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("Error");
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
这里有两个选项:
数据库触发器。这是在更改表时在数据库服务器上运行的代码。通常这些触发器是SQL语句,但是此博客(https://patternbuffer.wordpress.com/2012/09/14/triggering-shell-script-from-mysql/)发布了一个插件,用于讨论支持运行脚本的插件,该脚本可以发送MQTT消息,通知新用户上线。
使用MQTT消息和最后遗嘱和遗嘱功能。用户会将消息发布到已知主题然后他们上线,他们会在离线时发布类似的消息。如果连接因问题而丢失,则LWT可用于自动发布脱机消息。 (详情请见http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament