public async Task <IActionResult> Index(string key, double jd = 123.373345, double wd = 41.874623) { if (key == "liuzhe") { // 设备编码 测试: 1125132097 线上有数据: 1124347989 var received = new MqttMessageReceived { Topic = "cnc_sbm/gps_card", Payload = "{\"pkid\":\"4475EEDC2BB74F7C8FA6A59CEDB64720\",\"dev_id\":\"1124347989\",\"sxc_zt\":\"1\",\"jd\":\"123.354068\",\"wd\":\"41.857727\",\"gd\":\"0\",\"card_num\":\"1\",\"card_list\":[\"3603631297\"]}", QoS = new MQTTnet.Protocol.MqttQualityOfServiceLevel(), Retain = false }; await _schoolBusBusines.MqttMessageReceivedAsync(received); } await _chatHub.Clients.Group("3603631297").SendAsync("ReceiveMessage", jd, wd); return(View()); }
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); } }