Skip to content

Instantly share code, notes, and snippets.

@r3-yamauchi
Last active January 12, 2018 11:49
Show Gist options
  • Select an option

  • Save r3-yamauchi/635a8cc02d2666b1702814548f156cd1 to your computer and use it in GitHub Desktop.

Select an option

Save r3-yamauchi/635a8cc02d2666b1702814548f156cd1 to your computer and use it in GitHub Desktop.
AWS Lambda から AWS IoT に publish したメッセージを C# で MQTTライブラリを使って受信する https://blog.r3it.com/
using System;
using System.Diagnostics;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
private const string IotEndpoint = "endpoint.iot.ap-northeast-1.amazonaws.com";
private MqttClient client;
private void connectMqtt()
{
var caCert = new X509Certificate(Properties.Resources.root);
var clientCert = new X509Certificate2(Properties.Resources.pfx, @"");
client = new MqttClient(IotEndpoint, 8883, true, caCert, clientCert, MqttSslProtocols.TLSv1_2);
client.MqttMsgPublishReceived += ClientMqttMsgPublishReceived;
client.Subscribe(new[] { @"topic_1" }, new[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
var ret = client.Connect(Guid.NewGuid().ToString());
Debug.WriteLine("Connected with result code {0}", ret);
//while (client.IsConnected)
//{
//}
}
public static void ClientMqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
var msg = Encoding.UTF8.GetString(e.Message);
var topic = e.Topic;
Debug.WriteLine(topic + ", " + msg);
System.Diagnostics.Process.Start("https://blog.r3it.com/");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment