Last active
May 17, 2016 23:11
-
-
Save rafaelrmou/d3856e119a1060d12dd388d7433d6110 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
public class ViewMqtt : ContentPage{ | |
void Btn_Clicked (object sender, EventArgs e) | |
{ | |
var client = DependencyService.Get<IFMqttClient> (); | |
client.Connect ("borkerLink"); | |
client.Publish ("topicoQueRecebeMensagem", Encoding.UTF8.GetBytes ("mensagemEnviada")); | |
var topics = new string[] { | |
"Topico1 a ser assinado (subscribe)" | |
}; | |
foreach (var item in topics) { | |
Xamarin.Forms.MessagingCenter.Subscribe<MqqtReceived> (this, item, (t) => myByteAction (t)); | |
} | |
client.Subscribe (topics); | |
} | |
public void myByteAction (MqqtReceived ReceivedObject) | |
{ | |
var msg = Encoding.UTF8.GetString (ReceivedObject.Message, 0, ReceivedObject.Message.Length); | |
Device.BeginInvokeOnMainThread (() => { | |
switch ("Topico1 a ser assinado (subscribe)") { | |
case Constants.GeneralTopic: | |
//JsonConvert to Object | |
var surf = Newtonsoft.Json.JsonConvert.DeserializeObject<Surfboard> (msg); | |
System.Diagnostics.Debug.WriteLine (surf); | |
break; | |
default: | |
System.Diagnostics.Debug.WriteLine ("Mensagem recebida da IoT: " + msg); | |
break; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment