Created
May 17, 2016 23:01
-
-
Save rafaelrmou/16f90e3687a37abe9955bfda12edb2a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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