C# kodu
C#'ta bağlanın, abone olun ve yayınlayın
esegece.sgcWebSockets NuGet paketini (ya da esegece.sgcWebSockets.Community) kurun, ardından dört adımı izleyin.
using esegece.sgcWebSockets;
var client = new TsgcWebSocketClient();
client.Host = "broker.example.com";
client.Port = 8883;
client.TLS = true;
client.TLSOptions.Version = TwsTLSVersions.tls1_2;
client.Specifications.RFC6455 = false;
var mqtt = new TsgcWSPClient_MQTT();
mqtt.Client = client;
mqtt.MQTTVersion = TwsMQTTVersion.mqtt311;
mqtt.Authentication.Enabled = true;
mqtt.Authentication.UserName = "device-01";
mqtt.Authentication.Password = "secret";
mqtt.HeartBeat.Enabled = true;
mqtt.HeartBeat.Interval = 30;
mqtt.OnMQTTConnect += (conn, session, reasonCode, reasonName, props) =>
Console.WriteLine("#mqtt connected: " + reasonName);
mqtt.OnMQTTDisconnect += (conn, reasonCode, reasonName, props) =>
Console.WriteLine("#mqtt disconnected: " + reasonName);
client.Active = true;
mqtt.OnMQTTConnect += (conn, session, reasonCode, reasonName, props) =>
{
mqtt.Subscribe("sensors/+/temperature");
mqtt.Subscribe("alerts/#", TmqttQoS.mtqsExactlyOnce, null);
};
mqtt.OnMQTTSubscribe += (conn, packetId, codes, props) =>
Console.WriteLine("#subscribed, packet " + packetId);
mqtt.OnMQTTPublish += (conn, topic, text, props) =>
Console.WriteLine(topic + " = " + text);
mqtt.UnSubscribe("alerts/#");
mqtt.Publish("sensors/room1/temperature", "21.5");
mqtt.Publish("devices/device-01/status", "online", TmqttQoS.mtqsAtLeastOnce, true);
bool ok = mqtt.PublishAndWait("orders/new", "{\"id\": 42}", TmqttQoS.mtqsExactlyOnce, 10000);
mqtt.Publish("images/cam1", File.ReadAllBytes("frame.jpg"));
mqtt.OnMQTTPubAck += (conn, packetId, reasonCode, reasonName, props) =>
Console.WriteLine("#puback " + packetId);
mqtt.OnMQTTPubComp += (conn, packetId, reasonCode, reasonName, props) =>
Console.WriteLine("#pubcomp " + packetId);
using esegece.sgcWebSockets;
var client = new TsgcWebSocketClient();
client.Host = "broker.example.com";
client.Port = 443;
client.TLS = true;
client.Options.Parameters = "/mqtt";
client.WatchDog.Enabled = true;
var mqtt = new TsgcWSPClient_MQTT();
mqtt.Client = client;
mqtt.OnMQTTPublish += (conn, topic, text, props) => Console.WriteLine(topic + ": " + text);
client.Active = true;