Sample code
This topic provides sample code for the server-side .NET SDK for Message Queue for MQTT.
Demo
using System.Diagnostics.Tracing;
using System.Text;
using AliyunOnsmqttServerSdk;
using AliyunOnsmqttServerSdk.Common;
using AliyunOnsmqttServerSdk.Config;
using AliyunOnsmqttServerSdk.Model;
class Program
{
static void Main(string[] args)
{
// The server-side endpoint for Message Queue for MQTT.
string domain = "post-cn-******-server-internet.mqtt.aliyuncs.com";
// The ID of the Message Queue for MQTT instance.
string instanceId = "post-cn-******";
// The AccessKey ID.
string ak = "******";
// The AccessKey secret.
string sk = "******";
// The parent topic.
string firstTopic = "topicA";
// The group ID.
string gid = "GID-test";
int port = 5672;
ChannelConfig channelConfig = new ChannelConfig(domain, instanceId, ak, sk, port);
ServerConsumer serverConsumer = new ServerConsumer(channelConfig);
serverConsumer.Start();
// Subscribe to a parent topic.
serverConsumer.SubscribeTopic(firstTopic,
(string msgId, MessageProperties messageProperties, byte[] bArr) =>
{
Console.WriteLine($"recv:{msgId}");
});
// Subscribe to client online and offline events.
serverConsumer.SubscribeStatus(gid,
(statusNotice) =>{
Console.WriteLine($"recv: {statusNotice.ClientId},{statusNotice.EventType}");
});
ProducerConfig producerConfig = new ProducerConfig();
ServerProducer serverProducer = new ServerProducer(channelConfig, producerConfig);
serverProducer.Start();
string s = "test";
Encoding encoding = Encoding.UTF8;
byte[] payload = encoding.GetBytes(s);
try
{
for(; ; )
{
Thread.Sleep(1000);
SendResult sendResult = serverProducer.SendMessage(firstTopic+"/t2", payload);
Console.WriteLine($"send: {sendResult.MsgId}");
}
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
}Parameters
Parameter | Description |
domain | The endpoint for the Message Queue for MQTT instance. Clients use this endpoint to connect to the Message Queue for MQTT broker. When connecting to Message Queue for MQTT with the server-side SDK, use one of the following endpoint formats: Important Server-side SDK access is supported only for instances with kernel version V3.3.0 or later that are deployed in the Chinese mainland.
You can find the instance ID in the Basic Information section on the Instance Details page of the Message Queue for MQTT console. |
instanceId | The ID of the instance you created on the Message Queue for MQTT console. You can find this ID in the Basic Information section on the Instance Details page of the Message Queue for MQTT console. |
accessKey | The AccessKey ID used for authentication. For instructions on how to create one, see Create an AccessKey pair. Note To prevent credential leaks, store the AccessKey ID and AccessKey secret in environment variables or a configuration file rather than hard-coding them. |
secretKey | The AccessKey secret used for authentication. For instructions on how to create one, see Create an AccessKey pair. Note To prevent credential leaks, store the AccessKey ID and AccessKey secret in environment variables or a configuration file rather than hard-coding them. |
firstTopic | The parent topic you created on the Message Queue for MQTT console. |
gid | The group ID you created on the Message Queue for MQTT console. |
port | The protocol port. The port must match the protocol used. For the server-side SDK, this value is fixed to 5672. |