Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Started Program to start broker...");

            var mqttOptions = new MqttConfiguration
            {
                BufferSize                   = 128 * 1024,
                Port                         = 1883,
                KeepAliveSecs                = 10,
                WaitTimeoutSecs              = 2,
                MaximumQualityOfService      = MqttQualityOfService.AtMostOnce,
                AllowWildcardsInTopicFilters = true,
                AllowLocationSubscription    = true
            };

            var broker = MqttServer.Create(mqttOptions, authenticationProvider: RedisAuthenticationProvider.Instance);

            /** use constructor with Create( MqttConfiguration configuration, IMqttServerBinding binding = null, IMqttAuthenticationProvider authenticationProvider = null) */
            try
            {
                broker.Start();
                Console.WriteLine("Press CTRL + C exit.");
                while (true)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Exemple #2
0
        public async void Start()
        {
            var configuration = new MqttConfiguration
            {
                BufferSize                   = 128 * 1024,
                Port                         = 55555,
                KeepAliveSecs                = 10,
                WaitTimeoutSecs              = 2,
                MaximumQualityOfService      = MqttQualityOfService.AtMostOnce,
                AllowWildcardsInTopicFilters = true,
            };
            //var     = await MqttClient.CreateAsync("192.168.1.10", configuration);

            IMqttServer mqttServer = MqttServer.Create(configuration);

            mqttServer.ClientConnected    += MqttServer_ClientConnected;
            mqttServer.ClientDisconnected += MqttServer_ClientDisconnected;
            mqttServer.MessageUndelivered += MqttServer_MessageUndelivered;
            mqttServer.Start();


            client = await mqttServer.CreateClientAsync();

            // client.PublishAsync();


            Console.WriteLine("Hello World!");
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Debug.WriteLine("Configuring Server...");

            var stopSignal = new ManualResetEventSlim();
            var config     = new MqttConfiguration
            {
                Port = port,
                MaximumQualityOfService      = MqttQualityOfService.AtLeastOnce,
                AllowWildcardsInTopicFilters = true,
                WaitTimeoutSecs = 10,
                KeepAliveSecs   = 15
            };
            var server = MqttServer.Create(config);

            Debug.WriteLine("Starting Server...");

            server.Stopped += (sender, e) =>
            {
                Debug.WriteLine("Server stopped. Finishing app...");
                stopSignal.Set();
            };
            server.ClientConnected    += (sender, e) => Debug.WriteLine($"New player connected: {e}");
            server.ClientDisconnected += (sender, e) => Debug.WriteLine($"Player disconnected: {e}");

            server.Start();

            Debug.WriteLine("Server started...");
            Debug.WriteLine("Listening for new players...");

            stopSignal.Wait();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("MQTT Host v1.0");

            IMqttServer server = MqttServer.Create(new MqttConfiguration());

            server.ClientConnected += Server_ClientConnected;
            server.Start();

            System.Console.WriteLine("Server started.");
            Thread.Sleep(int.MaxValue);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Debug.WriteLine("Configuring Server...");

            var stopSignal = new ManualResetEventSlim();
            var config     = new MqttConfiguration
            {
                Port = port,
                MaximumQualityOfService      = MqttQualityOfService.AtLeastOnce,
                AllowWildcardsInTopicFilters = true,
                WaitTimeoutSecs = 10,
                KeepAliveSecs   = 15
            };
            var server = MqttServer.Create(config);

            Debug.WriteLine("Starting Server...");

            server.Stopped += (sender, e) =>
            {
                Debug.WriteLine("Server stopped. Finishing app...");
                stopSignal.Set();
            };
            server.ClientConnected    += (sender, e) => Debug.WriteLine($"New player connected: {e}");
            server.ClientDisconnected += (sender, e) => Debug.WriteLine($"Player disconnected: {e}");

            server.Start();

            var client = server.CreateClientAsync().Result;

            client.MessageStream.Subscribe(message =>
            {
                var text = Encoding.UTF8.GetString(message.Payload);

                Debug.WriteLine(string.Format("New message received to topic {0}: {1}", message.Topic, text));
            });

            client.SubscribeAsync("#", MqttQualityOfService.AtLeastOnce);

            Debug.WriteLine("Server started...");
            Debug.WriteLine("Listening for new players...");

            stopSignal.Wait();
        }
        static void Main(string[] args)
        {
            bool isFinishing     = false;
            var  deviceIpAddress = GetLocalIPAddress();
            var  config          = new MqttConfiguration {
                Port = 55555
            };
            var server = MqttServer.Create(config);

            server.Start();

            var client   = server.CreateClientAsync().Result;
            var clientId = client.Id;
            var received = "";

            client.SubscribeAsync(topic, MqttQualityOfService.AtLeastOnce).Wait();
            client.MessageStream.Subscribe(message => {
                if (isFinishing)
                {
                    return;
                }
                var data = Encoding.UTF8.GetString(message.Payload).Split(new string[] { ":" }, StringSplitOptions.None);
                Console.WriteLine($"Message Received from {data[0]}: {data[1]}");

                received = data[1];

                PublishAsync(client, clientId, exitMessage);

                if (received == exitMessage)
                {
                    isFinishing = true;
                }
            });

            Console.WriteLine($"Server {deviceIpAddress}:{config.Port} with topic '{topic}' is up.");
            Console.WriteLine($"Awaiting messages...");

            while (received != exitMessage)
            {
                Thread.Sleep(loopDelayTime);
            }
            Console.WriteLine("Shutting down... Received exit command.");
        }
        protected async Task <IMqttServer> GetServerAsync(IMqttAuthenticationProvider authenticationProvider = null)
        {
            try {
                LoadConfiguration();

                var server = MqttServer.Create(Configuration, authenticationProvider: authenticationProvider);

                server.Start();

                return(server);
            } catch (MqttException protocolEx) {
                if (protocolEx.InnerException is SocketException)
                {
                    return(await GetServerAsync());
                }
                else
                {
                    throw;
                }
            }
        }
        public void Setup(int port)
        {
            MqttConfiguration config = new MqttConfiguration()
            {
                AllowWildcardsInTopicFilters = true,
                ConnectionTimeoutSecs        = 10,
                KeepAliveSecs           = 10,
                WaitTimeoutSecs         = 10,
                Port                    = port,
                MaximumQualityOfService = MqttQualityOfService.AtLeastOnce
            };

            _server = MqttServer.Create(config, null, _authProv);

            _authProv.AddClientToAuth("PiezoPlayer", "player", "piezopass");
            _authProv.AddClientToAuth("Frontend", "front", "frontpass");
            _authProv.AddClientToAuth("FlickClient", "flick", "flickpass");
            _server.MessageUndelivered += _server_MessageUndelivered;
            _server.ClientConnected    += _server_ClientConnected;
            _server.ClientDisconnected += _server_ClientDisconnected;
            _server.Stopped            += _server_Stopped;
            _server.Start();
        }
 public void Setup()
 {
     Server = MqttServer.Create(Configuration, MqttServerBinding, _authenticationProvider);
     Server.Start();
 }