First steps. Hello World

Once the package is installed let’s go with a very basic example created as a console project:

namespace abetooTest
{
 class Program
 {
 static void Main(string[] args)
 {
 // Connection parameters
 const String myChannel =
 "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
 "735acf9cd6eda96e66ee3858496dca59d750aff1";
 
 const int heartBeat = 4;

 // Connection
 AbtooConnection abconnection = new AbtooConnection(myChannel, heartBeat);
 abconnection.connect();

 // Messages Listener
 abconnection.messageReceived += ((sender, abtoomsg) =>
 {
 AbtooMessage abmsg = (AbtooMessage)abtoomsg;
 Console.WriteLine(abmsg.body);
 });

 Console.ReadLine();
 }
 }
}

Let’s check it out the code a little bit deeper.

First at all we have to define the connection parameters, there are only 2 parameters so It’s very simple. We have to introduce a valid channel id string¬†and a heartbeat timeout in minutes.

// Connection parameters
 const String myChannel =
 "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
 "735acf9cd6eda96e66ee3858496dca59d750aff1";
 
 const int heartBeat = 4;
  • channel id define our local channel wich we are listening to.
  • heartbeat is used to monitor the channel health and to reestablish the connection if it was lost.

Then, once we’ve defined this parameters we can instantiate a new AbtooConnection object and launch the connection.

// Connection
 AbtooConnection abconnection = new AbtooConnection(myChannel, heartBeat);
 abconnection.connect();

Lastly we define a Listener to catch all the incoming messages.


// Messages Listener
abconnection.messageReceived += ((sender, abtoomsg) =>
{
AbtooMessage abmsg = (AbtooMessage)abtoomsg;
Console.WriteLine(abmsg.body);
});

Whenever we receive a new message the listener is triggered and we see the body message in the screen.

Notice that the body message is coded as a string so it’s easy to send serialized data like XML or JSON.

Related Articles

Leave A Comment?