Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

685 wiersze
23 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.ObjectModel;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Windows.Security.Cryptography.Certificates;
  8. using Windows.UI.Core;
  9. using Windows.UI.Xaml;
  10. using MQTTnet.Client;
  11. using MQTTnet.Diagnostics;
  12. using MQTTnet.Exceptions;
  13. using MQTTnet.Extensions.ManagedClient;
  14. using MQTTnet.Extensions.Rpc;
  15. using MQTTnet.Implementations;
  16. using MQTTnet.Protocol;
  17. using MQTTnet.Server;
  18. using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs;
  19. using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs;
  20. namespace MQTTnet.TestApp.UniversalWindows
  21. {
  22. public sealed partial class MainPage
  23. {
  24. private readonly ConcurrentQueue<MqttNetLogMessage> _traceMessages = new ConcurrentQueue<MqttNetLogMessage>();
  25. private readonly ObservableCollection<IMqttClientSessionStatus> _sessions = new ObservableCollection<IMqttClientSessionStatus>();
  26. private IMqttClient _mqttClient;
  27. private IManagedMqttClient _managedMqttClient;
  28. private IMqttServer _mqttServer;
  29. public MainPage()
  30. {
  31. InitializeComponent();
  32. ClientId.Text = Guid.NewGuid().ToString("D");
  33. MqttNetGlobalLogger.LogMessagePublished += OnTraceMessagePublished;
  34. }
  35. private async void OnTraceMessagePublished(object sender, MqttNetLogMessagePublishedEventArgs e)
  36. {
  37. _traceMessages.Enqueue(e.TraceMessage);
  38. await UpdateLogAsync();
  39. }
  40. private async Task UpdateLogAsync()
  41. {
  42. while (_traceMessages.Count > 100)
  43. {
  44. _traceMessages.TryDequeue(out _);
  45. }
  46. var logText = new StringBuilder();
  47. foreach (var traceMessage in _traceMessages)
  48. {
  49. logText.AppendFormat(
  50. "[{0:yyyy-MM-dd HH:mm:ss.fff}] [{1}] [{2}] [{3}] [{4}]{5}",
  51. traceMessage.Timestamp,
  52. traceMessage.Level,
  53. traceMessage.Source,
  54. traceMessage.ThreadId,
  55. traceMessage.Message,
  56. Environment.NewLine);
  57. if (traceMessage.Exception != null)
  58. {
  59. logText.AppendLine(traceMessage.Exception.ToString());
  60. }
  61. }
  62. await Trace.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
  63. {
  64. Trace.Text = logText.ToString();
  65. });
  66. }
  67. private async void Connect(object sender, RoutedEventArgs e)
  68. {
  69. var tlsOptions = new MqttClientTlsOptions
  70. {
  71. UseTls = UseTls.IsChecked == true,
  72. IgnoreCertificateChainErrors = true,
  73. IgnoreCertificateRevocationErrors = true,
  74. AllowUntrustedCertificates = true
  75. };
  76. var options = new MqttClientOptions
  77. {
  78. ClientId = ClientId.Text
  79. };
  80. if (UseTcp.IsChecked == true)
  81. {
  82. options.ChannelOptions = new MqttClientTcpOptions
  83. {
  84. Server = Server.Text,
  85. Port = int.Parse(Port.Text),
  86. TlsOptions = tlsOptions
  87. };
  88. }
  89. if (UseWs.IsChecked == true)
  90. {
  91. options.ChannelOptions = new MqttClientWebSocketOptions
  92. {
  93. Uri = Server.Text,
  94. TlsOptions = tlsOptions
  95. };
  96. }
  97. if (options.ChannelOptions == null)
  98. {
  99. throw new InvalidOperationException();
  100. }
  101. options.Credentials = new MqttClientCredentials
  102. {
  103. Username = User.Text,
  104. Password = Password.Text
  105. };
  106. options.CleanSession = CleanSession.IsChecked == true;
  107. options.KeepAlivePeriod = TimeSpan.FromSeconds(double.Parse(KeepAliveInterval.Text));
  108. try
  109. {
  110. if (_mqttClient != null)
  111. {
  112. await _mqttClient.DisconnectAsync();
  113. _mqttClient.ApplicationMessageReceived -= OnApplicationMessageReceived;
  114. _mqttClient.Connected -= OnConnected;
  115. _mqttClient.Disconnected -= OnDisconnected;
  116. }
  117. var factory = new MqttFactory();
  118. if (UseManagedClient.IsChecked == true)
  119. {
  120. _managedMqttClient = factory.CreateManagedMqttClient();
  121. _managedMqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;
  122. _managedMqttClient.Connected += OnConnected;
  123. _managedMqttClient.Disconnected += OnDisconnected;
  124. await _managedMqttClient.StartAsync(new ManagedMqttClientOptions
  125. {
  126. ClientOptions = options
  127. });
  128. }
  129. else
  130. {
  131. _mqttClient = factory.CreateMqttClient();
  132. _mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;
  133. _mqttClient.Connected += OnConnected;
  134. _mqttClient.Disconnected += OnDisconnected;
  135. await _mqttClient.ConnectAsync(options);
  136. }
  137. }
  138. catch (Exception exception)
  139. {
  140. Trace.Text += exception + Environment.NewLine;
  141. }
  142. }
  143. private void OnDisconnected(object sender, MqttClientDisconnectedEventArgs e)
  144. {
  145. _traceMessages.Enqueue(new MqttNetLogMessage("", DateTime.Now, -1,
  146. "", MqttNetLogLevel.Info, "! DISCONNECTED EVENT FIRED", null));
  147. Task.Run(UpdateLogAsync);
  148. }
  149. private void OnConnected(object sender, MqttClientConnectedEventArgs e)
  150. {
  151. _traceMessages.Enqueue(new MqttNetLogMessage("", DateTime.Now, -1,
  152. "", MqttNetLogLevel.Info, "! CONNECTED EVENT FIRED", null));
  153. Task.Run(UpdateLogAsync);
  154. }
  155. private async void OnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs eventArgs)
  156. {
  157. var item = $"Timestamp: {DateTime.Now:O} | Topic: {eventArgs.ApplicationMessage.Topic} | Payload: {Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)} | QoS: {eventArgs.ApplicationMessage.QualityOfServiceLevel}";
  158. await Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
  159. {
  160. if (AddReceivedMessagesToList.IsChecked == true)
  161. {
  162. ReceivedMessages.Items.Add(item);
  163. }
  164. });
  165. }
  166. private async void Publish(object sender, RoutedEventArgs e)
  167. {
  168. try
  169. {
  170. var qos = MqttQualityOfServiceLevel.AtMostOnce;
  171. if (QoS1.IsChecked == true)
  172. {
  173. qos = MqttQualityOfServiceLevel.AtLeastOnce;
  174. }
  175. if (QoS2.IsChecked == true)
  176. {
  177. qos = MqttQualityOfServiceLevel.ExactlyOnce;
  178. }
  179. var payload = new byte[0];
  180. if (PlainText.IsChecked == true)
  181. {
  182. payload = Encoding.UTF8.GetBytes(Payload.Text);
  183. }
  184. if (Base64.IsChecked == true)
  185. {
  186. payload = Convert.FromBase64String(Payload.Text);
  187. }
  188. var message = new MqttApplicationMessageBuilder()
  189. .WithTopic(Topic.Text)
  190. .WithPayload(payload)
  191. .WithQualityOfServiceLevel(qos)
  192. .WithRetainFlag(Retain.IsChecked == true)
  193. .Build();
  194. if (_mqttClient != null)
  195. {
  196. await _mqttClient.PublishAsync(message);
  197. }
  198. if (_managedMqttClient != null)
  199. {
  200. await _managedMqttClient.PublishAsync(message);
  201. }
  202. }
  203. catch (Exception exception)
  204. {
  205. Trace.Text += exception + Environment.NewLine;
  206. }
  207. }
  208. private async void Disconnect(object sender, RoutedEventArgs e)
  209. {
  210. try
  211. {
  212. if (_mqttClient != null)
  213. {
  214. await _mqttClient.DisconnectAsync();
  215. _mqttClient.Dispose();
  216. _mqttClient = null;
  217. }
  218. if (_managedMqttClient != null)
  219. {
  220. await _managedMqttClient.StopAsync();
  221. _managedMqttClient.Dispose();
  222. _managedMqttClient = null;
  223. }
  224. }
  225. catch (Exception exception)
  226. {
  227. Trace.Text += exception + Environment.NewLine;
  228. }
  229. }
  230. private void ClearLog(object sender, RoutedEventArgs e)
  231. {
  232. while (_traceMessages.Count > 0)
  233. {
  234. _traceMessages.TryDequeue(out _);
  235. }
  236. Trace.Text = string.Empty;
  237. }
  238. private async void Subscribe(object sender, RoutedEventArgs e)
  239. {
  240. try
  241. {
  242. var qos = MqttQualityOfServiceLevel.AtMostOnce;
  243. if (SubscribeQoS1.IsChecked == true)
  244. {
  245. qos = MqttQualityOfServiceLevel.AtLeastOnce;
  246. }
  247. if (SubscribeQoS2.IsChecked == true)
  248. {
  249. qos = MqttQualityOfServiceLevel.ExactlyOnce;
  250. }
  251. if (_mqttClient != null)
  252. {
  253. await _mqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos));
  254. }
  255. if (_managedMqttClient != null)
  256. {
  257. await _managedMqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos));
  258. }
  259. }
  260. catch (Exception exception)
  261. {
  262. Trace.Text += exception + Environment.NewLine;
  263. }
  264. }
  265. private async void Unsubscribe(object sender, RoutedEventArgs e)
  266. {
  267. try
  268. {
  269. if (_mqttClient != null)
  270. {
  271. await _mqttClient.UnsubscribeAsync(SubscribeTopic.Text);
  272. }
  273. if (_managedMqttClient != null)
  274. {
  275. await _managedMqttClient.UnsubscribeAsync(SubscribeTopic.Text);
  276. }
  277. }
  278. catch (Exception exception)
  279. {
  280. Trace.Text += exception + Environment.NewLine;
  281. }
  282. }
  283. // This code is for the Wiki at GitHub!
  284. // ReSharper disable once UnusedMember.Local
  285. private async void StartServer(object sender, RoutedEventArgs e)
  286. {
  287. if (_mqttServer != null)
  288. {
  289. return;
  290. }
  291. JsonServerStorage storage = null;
  292. if (ServerPersistRetainedMessages.IsChecked == true)
  293. {
  294. storage = new JsonServerStorage();
  295. if (ServerClearRetainedMessages.IsChecked == true)
  296. {
  297. storage.Clear();
  298. }
  299. }
  300. _mqttServer = new MqttFactory().CreateMqttServer();
  301. var options = new MqttServerOptions();
  302. options.DefaultEndpointOptions.Port = int.Parse(ServerPort.Text);
  303. options.Storage = storage;
  304. options.EnablePersistentSessions = ServerAllowPersistentSessions.IsChecked == true;
  305. await _mqttServer.StartAsync(options);
  306. }
  307. private async void StopServer(object sender, RoutedEventArgs e)
  308. {
  309. if (_mqttServer == null)
  310. {
  311. return;
  312. }
  313. await _mqttServer.StopAsync();
  314. _mqttServer = null;
  315. }
  316. private void ClearReceivedMessages(object sender, RoutedEventArgs e)
  317. {
  318. ReceivedMessages.Items.Clear();
  319. }
  320. private async void ExecuteRpc(object sender, RoutedEventArgs e)
  321. {
  322. var qos = MqttQualityOfServiceLevel.AtMostOnce;
  323. if (RpcQoS1.IsChecked == true)
  324. {
  325. qos = MqttQualityOfServiceLevel.AtLeastOnce;
  326. }
  327. if (RpcQoS2.IsChecked == true)
  328. {
  329. qos = MqttQualityOfServiceLevel.ExactlyOnce;
  330. }
  331. var payload = new byte[0];
  332. if (RpcText.IsChecked == true)
  333. {
  334. payload = Encoding.UTF8.GetBytes(RpcPayload.Text);
  335. }
  336. if (RpcBase64.IsChecked == true)
  337. {
  338. payload = Convert.FromBase64String(RpcPayload.Text);
  339. }
  340. try
  341. {
  342. var rpcClient = new MqttRpcClient(_mqttClient);
  343. var response = await rpcClient.ExecuteAsync(TimeSpan.FromSeconds(5), RpcMethod.Text, payload, qos);
  344. RpcResponses.Items.Add(RpcMethod.Text + " >>> " + Encoding.UTF8.GetString(response));
  345. }
  346. catch (MqttCommunicationTimedOutException)
  347. {
  348. RpcResponses.Items.Add(RpcMethod.Text + " >>> [TIMEOUT]");
  349. }
  350. catch (Exception exception)
  351. {
  352. RpcResponses.Items.Add(RpcMethod.Text + " >>> [EXCEPTION (" + exception.Message + ")]");
  353. }
  354. }
  355. private void ClearRpcResponses(object sender, RoutedEventArgs e)
  356. {
  357. RpcResponses.Items.Clear();
  358. }
  359. private void ClearSessions(object sender, RoutedEventArgs e)
  360. {
  361. _sessions.Clear();
  362. }
  363. private async void RefreshSessions(object sender, RoutedEventArgs e)
  364. {
  365. if (_mqttServer == null)
  366. {
  367. return;
  368. }
  369. var sessions = await _mqttServer.GetClientSessionsStatusAsync();
  370. _sessions.Clear();
  371. foreach (var session in sessions)
  372. {
  373. _sessions.Add(session);
  374. }
  375. ListViewSessions.DataContext = _sessions;
  376. }
  377. #region Wiki Code
  378. private async Task WikiCode()
  379. {
  380. {
  381. // Write all trace messages to the console window.
  382. MqttNetGlobalLogger.LogMessagePublished += (s, e) =>
  383. {
  384. Console.WriteLine($">> [{e.TraceMessage.Timestamp:O}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Source}] [{e.TraceMessage.Level}]: {e.TraceMessage.Message}");
  385. if (e.TraceMessage.Exception != null)
  386. {
  387. Console.WriteLine(e.TraceMessage.Exception);
  388. }
  389. };
  390. }
  391. {
  392. // Use a custom identifier for the trace messages.
  393. var clientOptions = new MqttClientOptionsBuilder()
  394. .Build();
  395. }
  396. {
  397. // Create a new MQTT client.
  398. var factory = new MqttFactory();
  399. var mqttClient = factory.CreateMqttClient();
  400. {
  401. // Create TCP based options using the builder.
  402. var options = new MqttClientOptionsBuilder()
  403. .WithClientId("Client1")
  404. .WithTcpServer("broker.hivemq.com")
  405. .WithCredentials("bud", "%spencer%")
  406. .WithTls()
  407. .WithCleanSession()
  408. .Build();
  409. await mqttClient.ConnectAsync(options);
  410. }
  411. {
  412. // Use TCP connection.
  413. var options = new MqttClientOptionsBuilder()
  414. .WithTcpServer("broker.hivemq.com", 1883) // Port is optional
  415. .Build();
  416. }
  417. {
  418. // Use secure TCP connection.
  419. var options = new MqttClientOptionsBuilder()
  420. .WithTcpServer("broker.hivemq.com")
  421. .WithTls()
  422. .Build();
  423. }
  424. {
  425. // Use WebSocket connection.
  426. var options = new MqttClientOptionsBuilder()
  427. .WithWebSocketServer("broker.hivemq.com:8000/mqtt")
  428. .Build();
  429. await mqttClient.ConnectAsync(options);
  430. }
  431. {
  432. // Create TCP based options manually
  433. var options = new MqttClientOptions
  434. {
  435. ClientId = "Client1",
  436. Credentials = new MqttClientCredentials
  437. {
  438. Username = "bud",
  439. Password = "%spencer%"
  440. },
  441. ChannelOptions = new MqttClientTcpOptions
  442. {
  443. Server = "broker.hivemq.org",
  444. TlsOptions = new MqttClientTlsOptions
  445. {
  446. UseTls = true
  447. }
  448. },
  449. };
  450. }
  451. {
  452. // Subscribe to a topic
  453. await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("my/topic").Build());
  454. // Unsubscribe from a topic
  455. await mqttClient.UnsubscribeAsync("my/topic");
  456. // Publish an application message
  457. var applicationMessage = new MqttApplicationMessageBuilder()
  458. .WithTopic("A/B/C")
  459. .WithPayload("Hello World")
  460. .WithAtLeastOnceQoS()
  461. .Build();
  462. await mqttClient.PublishAsync(applicationMessage);
  463. }
  464. }
  465. // ----------------------------------
  466. {
  467. var options = new MqttServerOptions();
  468. options.ConnectionValidator = c =>
  469. {
  470. if (c.ClientId.Length < 10)
  471. {
  472. c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
  473. return;
  474. }
  475. if (c.Username != "mySecretUser")
  476. {
  477. c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  478. return;
  479. }
  480. if (c.Password != "mySecretPassword")
  481. {
  482. c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  483. return;
  484. }
  485. c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
  486. };
  487. var factory = new MqttFactory();
  488. var mqttServer = factory.CreateMqttServer();
  489. await mqttServer.StartAsync(options);
  490. Console.WriteLine("Press any key to exit.");
  491. Console.ReadLine();
  492. await mqttServer.StopAsync();
  493. }
  494. // ----------------------------------
  495. // For UWP apps:
  496. MqttTcpChannel.CustomIgnorableServerCertificateErrorsResolver = o =>
  497. {
  498. if (o.Server == "server_with_revoked_cert")
  499. {
  500. return new[] { ChainValidationResult.Revoked };
  501. }
  502. return new ChainValidationResult[0];
  503. };
  504. {
  505. // Start a MQTT server.
  506. var mqttServer = new MqttFactory().CreateMqttServer();
  507. await mqttServer.StartAsync(new MqttServerOptions());
  508. Console.WriteLine("Press any key to exit.");
  509. Console.ReadLine();
  510. await mqttServer.StopAsync();
  511. }
  512. {
  513. // Configure MQTT server.
  514. var optionsBuilder = new MqttServerOptionsBuilder()
  515. .WithConnectionBacklog(100)
  516. .WithDefaultEndpointPort(1884);
  517. var options = new MqttServerOptions
  518. {
  519. };
  520. options.ConnectionValidator = c =>
  521. {
  522. if (c.ClientId != "Highlander")
  523. {
  524. c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
  525. return;
  526. }
  527. c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
  528. };
  529. var mqttServer = new MqttFactory().CreateMqttServer();
  530. await mqttServer.StartAsync(optionsBuilder.Build());
  531. }
  532. {
  533. // Setup client validator.
  534. var options = new MqttServerOptions
  535. {
  536. ConnectionValidator = c =>
  537. {
  538. if (c.ClientId.Length < 10)
  539. {
  540. c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
  541. return;
  542. }
  543. if (c.Username != "mySecretUser")
  544. {
  545. c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  546. return;
  547. }
  548. if (c.Password != "mySecretPassword")
  549. {
  550. c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  551. return;
  552. }
  553. c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
  554. }
  555. };
  556. }
  557. {
  558. // Create a new MQTT server.
  559. var mqttServer = new MqttFactory().CreateMqttServer();
  560. }
  561. {
  562. // Setup and start a managed MQTT client.
  563. var options = new ManagedMqttClientOptionsBuilder()
  564. .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
  565. .WithClientOptions(new MqttClientOptionsBuilder()
  566. .WithClientId("Client1")
  567. .WithTcpServer("broker.hivemq.com")
  568. .WithTls().Build())
  569. .Build();
  570. var mqttClient = new MqttFactory().CreateManagedMqttClient();
  571. await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("my/topic").Build());
  572. await mqttClient.StartAsync(options);
  573. }
  574. }
  575. #endregion
  576. }
  577. }