How to use sango - C #
So far MQTT library's ppatierno had developed m2mqtt has been donated to the paho.
http://www.eclipse.org/paho/clients/dotnet/
This library is compatible with the following .Net platforms.
- .Net Framework (up to 4.5)
- .Net Compact Framework 3.5 & 3.9 (for Windows Embedded Compact 7/2013)
- .Net Micro Framework 4.2 & 4.3
- Mono (for Linux OS)
- WinRT platforms (Windows 8.1 and Windows Phone 8.1)
However, MQTT 3.1.1 is not supported at this time. (Sango also supports 3.1)
Although I have not confirmed, it may be possible to use MQTT even with Unity with this library.
1. Download library -------------------------------------------------------------------------------------------- ------
http://www.eclipse.org/paho/clients/dotnet/
You can download from.
However, since codeplex distributes it with NuGet, I think that it is easier to download from NuGet. (Search with m2mqtt)
2. Client implementation
Connect
class MQTT
{
private MqttClient client;
public void Connect(string brokerHostname, int brokerPort,
string userName, string password)
{
// SSL使用時はtrue、CAを指定
client = new MqttClient(brokerHostname, brokerPort, false,
null);
// clientidを生成
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId, userName, password);
}
Connect uPLibrary.Networking.M2Mqtt.MqttClient with new. In MqttClient, there is an overload such as specifying only the host name.
Subscribe
// メッセージ到着時に呼び出されるメソッド
private void onReceive(object sender,
uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
{
Console.WriteLine(e.Topic);
string msg = Encoding.UTF8.GetString(e.Message);
Console.WriteLine(msg);
}
public void Subscribe(string topic){
// callbackを登録
client.MqttMsgPublishReceived += this.onReceive;
client.Subscribe(new string[] { topic }, new byte[] {
MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
}
client.MqttMsgPublishReceived leave add a method to, Subscribe you.
Publish
public void Publish(string topic, string msg)
{
client.Publish(
topic, Encoding.UTF8.GetBytes(msg),
MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false); // retainが
false
}
Publish just a.
reference
- M2Mqtt Client
- http://m2mqtt.wordpress.com/m2mqtt_doc/