示例#1
0
        private static async Task <IMqttServer> CreateMqttServerAsync()
        {
            var appsettings = ReadAppSettingsConfiguration();
            // Configure MQTT server.
            var optionsBuilder = new MqttServerOptionsBuilder()
                                 .WithConnectionBacklog(100)
                                 .WithDefaultEndpointPort(appsettings.Port);

            var mqttServer = new MqttFactory().CreateMqttServer();

            mqttServer.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(e =>
            {
                MyConsole.Disconnected($"Client {e.ClientId} disconnected event fired.");
            });
            mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e =>
            {
                MyConsole.Connected($"Client {e.ClientId} connected event fired.");
            });
            await mqttServer.StartAsync(optionsBuilder.Build());

            return(mqttServer);
        }