实例化MqttServer以及委托实现MqttServer的各个事件 1 2 3 mqttServer =newMqttFactory().CreateMqttServer()asMqttServer; mqttServer.StartedHandler =newMqttServerStartedHandlerDelegate(OnMqttServerStarted); mqttServer.StoppedHandler =newMqttServerStoppedHandlerDelegate(OnMqttServerStopped); 下面是完成的实现代码:...
publicMqttServer mqttServer=null;publicasyncvoidStartMqttServer(){try{if(mqttServer==null){varconfig=ReadConfiguration();varoptionsBuilder=newMqttServerOptionsBuilder().WithDefaultEndpoint().WithDefaultEndpointPort(int.Parse(config["Port"].ToString())).WithConnectionValidator(c=>{varcurrentUser=config["...
{//MQTT 服务器端publicstaticIMqttServer mqttServer;//MQTT 客户端publicstaticIMqttClient mqttClient;staticvoidMain(string[] args) {if(args.Length !=1) { Console.WriteLine("usage [dotnet run server] start server daemon"); Console.WriteLine("usage [dotnet run client] start client daemon"); }...
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation fromhttp://mqtt.org/. Topics tlsmqttiotcsharptcpcommunicationnugetuwpnetcoreiot-platformmqtt-brokerbrokernetmqtt-clientmqt...
17 serverClass.StartMqttServer().Wait(); 18 Console.ReadLine(); 19 } 20 } 21 public static class Config 22 { 23 public static int Port { get; set; } = 1883; 24 public static string UserName { get; set; } = "Username";
public IMqttClientOptions option() { //连接到服务器前,获取所需要的MqttClientTcpOptions 对象的信息 var options = new MqttClientOptionsBuilder() .WithClientId(ClientId) // clientid是设备id .WithTcpServer(IP, Port) //onenet ip:183.230.40.39 port:6002 ...
2 changes: 1 addition & 1 deletion 2 NewLife.MQTT/MqttServer.cs Original file line numberDiff line numberDiff line change @@ -10,7 +10,7 @@ namespace NewLife.MQTT; public class MqttServer : NetServer<MqttSession> { /// 消息交换机 public MqttExchange Exchange { get; set; } public...
{ // MQTT 服务器端 public static IMqttServer mqttServer; // MQTT 客户端 public static IMqttClient mqttClient; static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("usage [dotnet run server] start server daemon"); Console.WriteLine("usage [dotnet run client] ...
// Create MQTT client options var options = new MqttClientOptionsBuilder() .WithTcpServer(broker, port) // MQTT broker address and port .WithCredentials(username, password) // Set username and password .WithClientId(clientId) .WithCleanSession() .WithTls( o => { // The used public broker...
创建服务端最简单的方式是采用 MqttServerFactory 对象的 CreateMqttServer 方法来实现,该方法需要一个 MqttServerOptions 参数。 varoptions =newMqttServerOptions();varmqttServer =newMqttServerFactory().CreateMqttServer(options); 通过上述方式创建了一个 IMqttServer 对象后,调用其 StartAsync 方法即可启动 MQTT ...