C#

Microsoft Windows [版本 6.1.7601]

版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Windows\system32>c:

C:\Windows\system32>cd..

C:\Windows>cd..

C:\>cd emqx

C:\emqx>cd bin

C:\emqx\bin>dir

驱动器 C 中的卷没有标签。

卷的序列号是 DA83-2A7F

C:\emqx\bin 的目录

2019/11/05  20:10    <DIR>          .

2019/11/05  20:10    <DIR>          ..

2019/08/04  12:59          304,973 cuttlefish

2019/08/04  12:53            19,192 emqx

2019/08/04  12:53            7,563 emqx.cmd

2019/08/04  12:53            2,416 emqx_ctl

2019/08/04  12:53            2,489 emqx_ctl.cmd

2019/08/04  12:53              468 emqx_env

2019/08/04  12:53            6,203 install_upgrade_escript

2019/08/04  12:53            12,433 nodetool

2019/08/04  13:01            30,360 no_dot_erlang.boot

2019/08/04  13:01            30,360 start_clean.boot

              10 个文件        416,457 字节

              2 个目录 47,299,125,248 可用字节

C:\emqx\bin>emqx

usage: emqx (install|uninstall|start|stop|restart|console|ping|list|attach)

C:\emqx\bin>emqx start

C:\emqx>emqx_ctl status

'emqx_ctl' 不是内部或外部命令,也不是可运行的程序

或批处理文件。

C:\emqx>cd bin

C:\emqx\bin>emqx_ctl status

Node 'emqx@127.0.0.1' is started

emqx 3.2.2 is running

C:\emqx\bin>cd..

C:\emqx>cd

C:\emqx

C:\emqx>cd..

C:\>ipconfig

Windows IP 配置

以太网适配器 本地连接:

  连接特定的 DNS 后缀 . . . . . . . :

  本地链接 IPv6 地址. . . . . . . . : fe80::fd2e:705d:5b79:8b4e%11

  IPv4 地址 . . . . . . . . . . . . : 192.168.221.58

  子网掩码  . . . . . . . . . . . . : 255.255.255.0

  默认网关. . . . . . . . . . . . . : 192.168.221.254

隧道适配器 isatap.{86AE2A60-5600-4E8E-918B-CA4D01D021E6}:

  媒体状态  . . . . . . . . . . . . : 媒体已断开

  连接特定的 DNS 后缀 . . . . . . . :

C:\>

网址http://localhost:18083/#/login?redirect=%2F

android:架包app  libs

public class MainActivity extends AppCompatActivity {

    String host = "tcp://192.168.221.58:1883";

    String userName = "admin";

    String userPwd = "public";

    MqttClient client;

    MqttConnectOptions options;

    String topic = "a/b/c";

    ScheduledExecutorService scheduledExecutorService;

    Handler handler;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        init();

        handler=new Handler()

        {

            @Override

            public void handleMessage(Message msg){

                super.handleMessage(msg);

                if(msg.what==0x01)

                {

                    Toast.makeText(MainActivity.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();

                }

                if(msg.what==0x02)

                {

                    Toast.makeText(MainActivity.this,"连接成功!",Toast.LENGTH_SHORT).show();

                    try {

                        client.subscribe(topic,1);

                    } catch (MqttException e) {

                        e.printStackTrace();

                    }

                }

                if(msg.what==0x03)

                {

                    Toast.makeText(MainActivity.this,"连接失败!",Toast.LENGTH_SHORT).show();

                }

            }

        };

        startReconnect();

    }

    public void init() {

        try {

            client = new MqttClient(host, "test01", new MemoryPersistence());

            options = new MqttConnectOptions();

            options.setCleanSession(true);

            options.setUserName(userName);

            options.setPassword(userPwd.toCharArray());

            options.setConnectionTimeout(10);

            options.setKeepAliveInterval(20);

            client.setCallback(new MqttCallback() {

                @Override

                public void connectionLost(Throwable throwable) {

                }

                @Override

                public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {

                    Message msg=new Message();

                    msg.what=0x01;

                    msg.obj=s+"--"+ mqttMessage.toString();

                    handler.sendMessage(msg);

                }

                @Override

                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

                }

            });

        } catch (MqttException e) {

            e.printStackTrace();

        }

    }

    public void connect()

    {

        new Thread(){

            @Override

            public void run(){

                try{

                    client.connect(options);

                    handler.sendEmptyMessage(0x02);

                }catch(MqttException e){

                    e.printStackTrace();

                    handler.sendEmptyMessage(0x03);

                }

            }

        }.start();

    }

    public void startReconnect()

    {

        scheduledExecutorService= Executors.newSingleThreadScheduledExecutor();

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

            @Override

            public void run() {

                if(!client.isConnected())

                {

                    connect();

                }

            }

        },1*1000,10*1000, TimeUnit.MILLISECONDS);

    }

}

C#加载库M2M

Form1.Designer.cs

        /// <summary>

        /// 必需的设计器变量。

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// 清理所有正在使用的资源。

        /// </summary>

        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows 窗体设计器生成的代码

        /// <summary>

        /// 设计器支持所需的方法 - 不要修改

        /// 使用代码编辑器修改此方法的内容。

        /// </summary>

        private void InitializeComponent()

        {

            this.button1 = new System.Windows.Forms.Button();

            this.txtReceiveMsg = new System.Windows.Forms.TextBox();

            this.SuspendLayout();

            //

            // button1

            //

            this.button1.Location = new System.Drawing.Point(109, 81);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(75, 23);

            this.button1.TabIndex = 0;

            this.button1.Text = "button1";

            this.button1.UseVisualStyleBackColor = true;

            //

            // txtReceiveMsg

            //

            this.txtReceiveMsg.Location = new System.Drawing.Point(65, 38);

            this.txtReceiveMsg.Name = "txtReceiveMsg";

            this.txtReceiveMsg.Size = new System.Drawing.Size(129, 21);

            this.txtReceiveMsg.TabIndex = 1;

            this.txtReceiveMsg.TextChanged += new System.EventHandler(this.txtReceiveMsg_TextChanged);

            //

            // Form1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(284, 262);

            this.Controls.Add(this.txtReceiveMsg);

            this.Controls.Add(this.button1);

            this.Name = "Form1";

            this.Text = "Form1";

            this.Load += new System.EventHandler(this.Form1_Load);

            this.ResumeLayout(false);

            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;

        private System.Windows.Forms.TextBox txtReceiveMsg;

    }

}

Form1.cs:

namespace MQTT

{

    public partial class Form1 : Form

    {

        string enpoint = "192.168.221.55";

        int port = 1883;

        string user = "admin";

        string pwd = "public";

        string clientid = Guid.NewGuid().ToString();

        MqttClient client;

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            try

            {

                client = new MqttClient(enpoint, port, false, MqttSslProtocols.None, null, null);

                client.Connect(clientid, user, pwd);

                if (client.IsConnected)

                {

                    MessageBox.Show("连接成功");

                    client.Subscribe(new string[] { "as" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });

                    client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

        private void ShowMsg(string msg)

        {

        txtReceiveMsg.AppendText(msg);

            }

        private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)

        {

            Action<string> showAct = new Action<string>(ShowMsg);

            Invoke(showAct, new string[] { Encoding.UTF8.GetString(e.Message) });

            //Invoke(new Action(() =>

          // {

          //    button1.AppendText(Encoding.UTF8.GetString(e.Message));

          // }

          // ));

        }

        private void txtReceiveMsg_TextChanged(object sender, EventArgs e)

        {

        }

    }

}

Text 命名txtReceiveMsg

button

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,270评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,489评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,630评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,906评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,928评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,718评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,442评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,345评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,802评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,984评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,117评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,810评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,462评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,011评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,139评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,377评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,060评论 2 355

推荐阅读更多精彩内容