Example #1
0
        public static async Task <MqttServerInstance> StartServer(MqttServerConfiguration serverConfiguration)
        {
            logger.Info($"Starting Mqtt Server");
            string storagefile = Path.Combine(PlugInData.HomeSeerDirectory, "data", PlugInData.PlugInId, "mqtt", "retained.json");

            // Configure MQTT server.
            var optionsBuilder = new MqttServerOptionsBuilder()
                                 .WithConnectionBacklog(512)
                                 .WithStorage(new MqttStorage(storagefile))
                                 .WithDefaultEndpointPort(serverConfiguration.Port);

            if (serverConfiguration.BoundIPAddress != null)
            {
                optionsBuilder = optionsBuilder.WithDefaultEndpointBoundIPAddress(serverConfiguration.BoundIPAddress);
            }
            optionsBuilder = optionsBuilder.WithDefaultEndpointBoundIPV6Address(IPAddress.None);

            var mqttServer = new MqttFactory(new MqttNetLogger()).CreateMqttServer();
            await mqttServer.StartAsync(optionsBuilder.Build()).ConfigureAwait(false);

            return(new MqttServerInstance(mqttServer, serverConfiguration));
        }
Example #2
0
 public MqttServerInstance(IMqttServer?server,
                           MqttServerConfiguration configuration)
 {
     this.Server        = server;
     this.Configuration = configuration;
 }