Пример #1
0
        static void Main(string[] args)
        {
            MqttNetTrace.TraceMessagePublished += MqttNetTrace_TraceMessagePublished;
            new Thread(StartMqttServer).Start();

            while (true)
            {
                var inputString = Console.ReadLine().ToLower().Trim();

                if (inputString == "exit")
                {
                    mqttServer?.StopAsync();
                    Console.WriteLine("MQTT服务已停止!");
                    break;
                }
                else if (inputString == "clients")
                {
                    foreach (var item in mqttServer.GetConnectedClients())
                    {
                        Console.WriteLine($"客户端标识:{item.ClientId},协议版本:{item.ProtocolVersion}");
                    }
                }
                else
                {
                    Console.WriteLine($"命令[{inputString}]无效!");
                }
            }
        }
Пример #2
0
        public static void StartServer()
        {
            MqttNetTrace.TraceMessagePublished += MqttNetTrace_TraceMessagePublished;
            new Thread(StartMqttServer).Start();

            while (true)
            {
                var inputString = Console.ReadLine().ToLower().Trim();

                if (inputString == "exit")
                {
                    mqttServer?.StopAsync();
                    Console.WriteLine("MQTT Stoped!");
                    break;
                }
                else if (inputString == "clients")
                {
                    foreach (var item in mqttServer.GetConnectedClients())
                    {
                        Console.WriteLine($"client:{item.ClientId},protocol version:{item.ProtocolVersion}");
                    }
                }
                else if (inputString == "publish")
                {
                    string topic        = "hellobo";
                    string inputString1 = "hello123123123";
                    var    appMsg       = new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes(inputString1), MqttQualityOfServiceLevel.AtMostOnce, false);
                    mqttServer.Publish(appMsg);
                }
                else
                {
                    Console.WriteLine($"command[{inputString}]Invalid!");
                }
            }
        }
Пример #3
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            string command = Console.ReadLine();

            if (command == null)
            {
                return;
            }
            var inputString = command.ToLower().Trim();

            if (inputString == "exit")
            {
                mqttServer.StopAsync();
                Console.WriteLine("MQTT service stop!");

                timer1.Enabled = false;
                mStartMqttServerThread.Abort();
                Console.WriteLine("MQTT service stop successfully!");
                return;
            }
            else if (inputString == "clients")
            {
                foreach (var item in mqttServer.GetConnectedClients())
                {
                    Console.WriteLine($"Cliend Id:{item.ClientId},Protocol Version:{item.ProtocolVersion}");
                }
            }
            else
            {
                Console.WriteLine($"Command [{inputString}] invalid!");
            }
        }
Пример #4
0
        public void GetConnectedClients(IApiCall apiCall)
        {
            var connectedClients = _server.GetConnectedClients();

            apiCall.Result["ConnectedClients"] = JToken.FromObject(connectedClients);
        }