We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
public async Task StartAsync() { var url = _configuration["MqttStrings:Url"]; if (!TryParse(_configuration["MqttStrings:Port"], out int port) || string.IsNullOrWhiteSpace(url)) return; var clientId = $"{_configuration["MqttStrings:ClientId"] ?? "IotServerClient"}_{new Random().Next(0, 9999)}"; _logger.LogInformation($"ClientId:{clientId}"); var mqttClientOptions = new MqttClientOptionsBuilder() .WithTcpServer(url, port) .WithClientId(clientId) .Build(); await _mqttClient.ConnectAsync(mqttClientOptions); ApplicationMessageReceived(); await SubscribeAsync(); Heartbeat(mqttClientOptions); } private void Heartbeat(MqttClientOptions mqttClientOptions) { _ = Task.Run( async () => { while (true) { try { if (await _mqttClient.TryPingAsync()) continue; await _mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None); ApplicationMessageReceived(); await SubscribeAsync(); } catch (Exception ex) { _logger.LogError($"【异常断线触发重连】错误信息:{ex.Message}"); } finally { // 每15秒检查一次连接状态,如果需要,执行一次重新连接。 await Task.Delay(TimeSpan.FromSeconds(15)); } } }); } private void ApplicationMessageReceived() { var semaphore = new SemaphoreSlim(80); _mqttClient.ApplicationMessageReceivedAsync += async arg => { await semaphore.WaitAsync(); _ = Task.Run(async () => { ......... }).ConfigureAwait(false); return; }; } public async Task SubscribeAsync() { ....... }
以上是我的链接代码。
The text was updated successfully, but these errors were encountered:
有解决办法么。我也遇到了
Sorry, something went wrong.
我也是,【/插眼】
No branches or pull requests
Describe your question
以上是我的链接代码。
Which project is your question related to?
The text was updated successfully, but these errors were encountered: