Skip to content

Instantly share code, notes, and snippets.

@rafaelrmou
Created May 17, 2016 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelrmou/16f90e3687a37abe9955bfda12edb2a9 to your computer and use it in GitHub Desktop.
Save rafaelrmou/16f90e3687a37abe9955bfda12edb2a9 to your computer and use it in GitHub Desktop.
[assembly: Xamarin.Forms.Dependency (typeof(FMqttClient))]
namespace Mqtt_Forms.iOS
{
public class FMqttClient : IFMqttClient
{
public MqttClient client {
get;
set;
}
public string Broker {
get;
set;
}
public FMqttClient ()
{
}
#region IFMqttClient implementation
public void Connect (string server)
{
try {
client = new MqttClient (server);
string clientId = Guid.NewGuid ().ToString ();
client.Connect (clientId);
Broker = server;
}//catch(uPLibrary.Exception.ser
catch (Exception ex) {
throw new Exception (ex.Message);
}
}
void client_MqttMsgPublishReceived (object sender, MqttMsgPublishEventArgs e)
{
if (e.Message.Any ()) {
var receive = new MqqtReceived () {
Broker = Broker,
Topic = e.Topic,
Message = e.Message
};
Xamarin.Forms.MessagingCenter.Send<MqqtReceived> (receive, e.Topic);
}
}
public void Publish (string service, byte[] command)
{
if (client.IsConnected)
client.Publish (service, command);
else
throw new Exception ("Client isn't connected");
}
public void Subscribe (string[] topics)
{
client.Subscribe (topics,
new byte[] {
MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE
});
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
client.MqttMsgSubscribed += Client_MqttMsgSubscribed;
client.MqttMsgUnsubscribed += Client_MqttMsgUnsubscribed;
client.MqttMsgPublished += Client_MqttMsgPublished;
}
void Client_MqttMsgPublished (object sender, MqttMsgPublishedEventArgs e)
{
}
void Client_MqttMsgUnsubscribed (object sender, MqttMsgUnsubscribedEventArgs e)
{
}
void Client_MqttMsgSubscribed (object sender, MqttMsgSubscribedEventArgs e)
{
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment