2.1.3.Install M2Mqtt by Nuget, Example code

  • 1.Requirement: .NET Framework 4.5

  • 2.Open Nuget console and input "Install-Package M2Mqtt"

  • 3.Example code:

    • 1.Publisher:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using uPLibrary.Networking.M2Mqtt;
      using uPLibrary.Networking.M2Mqtt.Messages;
      
      namespace MQTTPublisher
      {
          class Program
          {
              static void Main(string[] args)
              {
                  var id = args[0];
                  var topic = args[1];
      
                  MqttClient client = new MqttClient("127.0.0.1");
                  //若有輸入user, password, 接收端也要一樣的user, password
                  client.Connect(id);
      
                  //若要知道有沒有發送出去可以增加監聽發送後的Event
                  client.MqttMsgPublished += new MqttClient.MqttMsgPublishedEventHandler(client_MqttMsgPublished);
  • 2.Subscriber:

  • 4.心得:

    • 1.Publisher與Subscriber的ID不要相同

    • 2.Publisher與Subscriber的Topic要相同

Last updated

Was this helpful?