c#程序 MQTT协议发送消息。

MQTT协议接收/发送消息

  由于项目需要采用MQTT协议收/发送消息,首先购买了一个阿里云服务器,在其上安装 nginx服务器,安装Tomcat服务器,安装MQtt服务器。

  说明:要下载一个 M2Mqtt.dll 文件,将这个dll文件引用到项目中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

   namespace mqtt_test
{
    public partial class Form1 : Form
    {
        string clientId = "testclient_01";//测试客户端
        MqttClient client = null;

        public Form1()
        {
            InitializeComponent();
            //client = new MqttClient("tcp://
11x.xx.xx.xxx:8080");
            client = new MqttClient(IPAddress.Parse("
11x.xx.xx.xxx"), 8080, false, null);
            client.Connect(clientId, "admin", "password");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(textBox1.Text);
            client.Publish("/device", Encoding.UTF8.GetBytes(textBox1.Text), MqttMsgBase.QOS_LEVE
L_EXACTLY_ONCE, false);
        }
    }
}

 这样是没有加入认证文件的,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值