Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net.Mqtt;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- namespace XamFormsSample
- {
- public partial class MainPage : ContentPage
- {
- const string topic = "test/chat/message";
- //private HermesDataServices _mqttDataServices;
- public MainPage()
- {
- Connect();
- InitializeComponent();
- }
- private static void Connect()
- {
- var config = new MqttConfiguration {Port = 1883};
- var client = MqttClient.CreateAsync("192.168.1.222", config).Result;
- Debug.WriteLine($"client is a: {client.GetType()} type."); // jha
- var clientId = "XamarinClient";
- client.ConnectAsync(new MqttClientCredentials(clientId)).Wait();
- client.SubscribeAsync(topic, MqttQualityOfService.AtLeastOnce).Wait();
- client.MessageStream.Subscribe(message =>
- {
- var data = Encoding.UTF8.GetString(message.Payload);
- Console.WriteLine($"Message Received from {data}");
- });
- Console.WriteLine($"Client connected successfully to {client.Id}:{config.Port}");
- Console.WriteLine($"Awaiting messages...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment