• 大小: 45.32MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-07-24
  • 语言: Java
  • 标签: MQTT  

资源简介

1、利用apache-apollo-1.7.1创建MQTT broker 2、利用eclipse编写MQTT Server的JAVA代码 3、能够订阅(接收)和发布(发送)消息,带接收消息回显 4、该代码同时也可以作为客户端,只须做小小改动

资源截图

代码片段和文件信息

package MQTT_Server;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/*
import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
*/


import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;


public class MQTT_Server extends Jframe {
private static final long serialVersionUID = 1L;

private JPanel panel_mqtt_center;
private JPanel panel_mqtt_center_publish;
private JPanel panel_mqtt_center_subscribe;
private JScrollPane panel_mqtt_top;
private JScrollPane panel_mqtt_bottom;



//private JPanel panel_pd_list;
private JButton button_pub;
private JButton button_sub;

 
private MqttClient client;
//private String host = “tcp://0.0.0.0:61613“;
private String host = “tcp://localhost:61613“;
private String userName = “admin“;
private String passWord = “password“;
public static final String TOPIC_BEAT = “beat“;
private MqttTopic topic;
private MqttMessage message;
 

JTextArea ta_pd_details;
JTextArea ta_pubta_sub;
JTextField tf_pd_findtf_pub_topictf_sub_topic;
JTable pd_table;



public MQTT_Server() {

try {
client = new MqttClient(host “Server“
new MemoryPersistence());
connect();
} catch (Exception e) {
e.printStackTrace();
}
this.settitle(“MQTT SERVER“);

Container container = this.getContentPane();
//container.setLayout(new java.awt.FlowLayout());



//top
String s2=“发布内容!!“;
ta_pub=new JTextArea(s2520);
panel_mqtt_top = new JScrollPane(ta_pub);




//center
panel_mqtt_center = new JPanel();
panel_mqtt_center.setLayout(new BorderLayout(1010));

panel_mqtt_center_publish = new JPanel();
panel_mqtt_center_publish.setLayout(new BorderLayout());
tf_pub_topic = new JTextField(“mytopic“);
button_pub = new JButton(“发布话题“);
panel_mqtt_center_publish.add(button_pubBorderLayout.EAST);
panel_mqtt_center_publish.add(tf_pub_topic);

panel_mqtt_center_subscribe = new JPanel();
panel_mqtt_center_subscribe.setLayout(new BorderLayout());
tf_sub_topic = new JTextField(“订阅主题“);
button_sub = new JButton(“订阅话题“);
panel_mqtt_center_subscribe.add(button_subBorderLayout.EAST);
panel_mqtt_center_subscribe.add(tf_sub_topic);

panel_mqtt_center.add(panel_mqtt_center_publishBorderLayout.NORTH);
panel_mqtt_center.add(panel_mqtt_center_subscribeBorderLayout.CENTER);

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件   28110104  2016-12-06 11:56  MQTT_Server\apache-apollo-1.7.1-windows-distro.zip

     文件         74  2017-05-16 11:34  MQTT_Server\mqtt_broker_create.bat

     文件         40  2017-05-04 09:04  MQTT_Server\mqtt_broker_run.bat

     文件          0  2017-04-18 15:24  MQTT_Server\MQTT_Server\.metadata\.lock

     文件     172829  2017-05-19 16:55  MQTT_Server\MQTT_Server\.metadata\.log

     文件         20  2017-04-18 15:25  MQTT_Server\MQTT_Server\.metadata\.mylyn\.taskListIndex\segments.gen

     文件         32  2017-04-18 15:25  MQTT_Server\MQTT_Server\.metadata\.mylyn\.taskListIndex\segments_1

     文件        250  2017-05-19 16:22  MQTT_Server\MQTT_Server\.metadata\.mylyn\.tasks.xml.zip

     文件        438  2017-04-18 15:25  MQTT_Server\MQTT_Server\.metadata\.mylyn\repositories.xml.zip

     文件        250  2017-05-19 16:41  MQTT_Server\MQTT_Server\.metadata\.mylyn\tasks.xml.zip

     文件      14923  2017-05-19 13:43  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\0\30d4871b563c00171c65a5a3a30934ad

     文件      14851  2017-05-19 13:43  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\0\608eecf2573c00171c65a5a3a30934ad

     文件      12591  2017-05-15 11:00  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\12\60d7a4a01a3900171cd7cc6a8a3ef8fc

     文件      15473  2017-05-18 15:47  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\15\6074411c9f3b001713cfd8aebf3008d1

     文件      10205  2017-05-19 16:34  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\1a\90df18396e3c00171e5cd89dbf4b4e0f

     文件      12591  2017-05-15 11:00  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\20\909ec36b013a00171388ca2defef3177

     文件       9898  2017-05-19 16:36  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\21\a04faf596e3c00171e5cd89dbf4b4e0f

     文件      11119  2017-05-19 16:32  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\28\b0e2f1ce6d3c00171e5cd89dbf4b4e0f

     文件      15480  2017-05-19 11:33  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\2d\909b05e1543c00171c65a5a3a30934ad

     文件       7465  2017-05-19 16:41  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\31\a0580873703c00171e5cd89dbf4b4e0f

     文件      11032  2017-05-19 16:33  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\38\90c40cde6d3c00171e5cd89dbf4b4e0f

     文件      13544  2017-05-16 14:32  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\4\80a9fec1013a00171388ca2defef3177

     文件      15167  2017-05-19 13:56  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\4b\d0f71c43583c00171c65a5a3a30934ad

     文件      11755  2017-05-19 16:28  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\4f\4067049e6d3c00171e5cd89dbf4b4e0f

     文件       8517  2017-05-19 16:40  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\4f\701046e66e3c00171e5cd89dbf4b4e0f

     文件      15490  2017-05-18 15:41  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\52\40ff834c9e3b001713cfd8aebf3008d1

     文件      14851  2017-05-19 13:34  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\56\0082e9a5553c00171c65a5a3a30934ad

     文件       6593  2017-05-19 16:55  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\56\206c5d25713c00171e5cd89dbf4b4e0f

     文件      15453  2017-05-16 17:26  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\62\80b9fe9dce3a00171cbbc26b5b249f8c

     文件      15450  2017-05-17 15:01  MQTT_Server\MQTT_Server\.metadata\.plugins\org.eclipse.core.resources\.history\66\2027f9579d3b001713cfd8aebf3008d1

............此处省略426个文件信息

评论

共有 条评论