Ejemplo n.º 1
0
        public async Task ConnectMqttServerAsync()
        {
            // var logger = _loggerFactory.CreateLogger("MqttHelper");
            IMqttClientOptions MqttOptions()
            {
                var options = new MqttClientOptionsBuilder()
                              .WithClientId(_option.ClientID)
                              .WithTcpServer(_option.HostIp, _option.HostPort)
                              .WithCredentials(_option.UserName, _option.Password)
                              //.WithTls()//服务器端没有启用加密协议,这里用tls的会提示协议异常
                              .WithCleanSession()
                              .Build();

                return(options);
            }

            // Create a new Mqtt client.
            try
            {
                if (MqttClient == null)
                {
                    MqttClient = new MqttFactory().CreateMqttClient();

                    // 接收到消息回调
                    MqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
                    {
                        var received = new MqttMessageReceived
                        {
                            Topic   = e.ApplicationMessage.Topic,
                            Payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload),
                            QoS     = e.ApplicationMessage.QualityOfServiceLevel,
                            Retain  = e.ApplicationMessage.Retain
                        };
#if DEBUG
                        //const string path = "E:\\MQTTPayload.txt";
                        //await Tools.WriteTxt(path, received.Payload);
#endif

                        var messageQueue = new SenparcMessageQueue();
                        var key          = SenparcMessageQueue.GenerateKey("MessageHandlerSendMessageAsync", MqttClient.GetType(), Guid.NewGuid().ToString(), "MqttClient.ApplicationMessageReceivedHandler");
                        messageQueue.Add(key, async() =>
                        {
                            _logger.LogWarning("MqttReceivedMsg:\r\n" + received.Payload);
                            await _schoolBusBusines.MqttMessageReceivedAsync(received);
                        });

                        //Console.WriteLine($">> ### 接受消息 ###{Environment.NewLine}");
                        //Console.WriteLine($">> Topic = {received.Topic}{Environment.NewLine}");
                        //Console.WriteLine($">> Payload = {received.Payload}{Environment.NewLine}");
                        //Console.WriteLine($">> QoS = {received.QoS}{Environment.NewLine}");
                        //Console.WriteLine($">> Retain = {received.Retain}{Environment.NewLine}");
                    });

                    // 连接成功回调
                    MqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(async e =>
                    {
                        Console.WriteLine("已连接到MQTT服务器!" + Environment.NewLine);
                        await Subscribe(MqttClient, _option.MqttTopic);
                    });

                    // 断开连接回调
                    MqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(async e =>
                    {
                        var curTime = DateTime.UtcNow;
                        Console.WriteLine($">> [{curTime.ToLongTimeString()}]");
                        Console.WriteLine("已断开MQTT连接!" + Environment.NewLine);
                        //Reconnecting 重连
                        if (IsReconnect && !e.ClientWasConnected)
                        {
                            Console.WriteLine("正在尝试重新连接" + Environment.NewLine);
                            await Task.Delay(TimeSpan.FromSeconds(5));
                            try
                            {
                                await MqttClient.ConnectAsync(MqttOptions());
                            }
                            catch
                            {
                                Console.WriteLine("### 重新连接 失败 ###" + Environment.NewLine);
                            }
                        }
                        else
                        {
                            Console.WriteLine("已下线!" + Environment.NewLine);
                        }
                    });

                    try
                    {
                        await MqttClient.ConnectAsync(MqttOptions());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("连接到MQTT服务器失败!" + Environment.NewLine + ex.Message + Environment.NewLine);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("连接到MQTT服务器未知异常!" + Environment.NewLine + e.Message + Environment.NewLine);
            }
        }