Swin Messenger How To

From SwinBrain

In this article we will look at how to make simple Swin Messenger Client in C#. A Swin Messenger Client is an instant messaging client that can connect to any XMPP server.

This is a How To article designed to give step-by-step instructions. Search SwinBrain and the external links if you require more detailed information about this topic.

Contents

Getting the Resources

The first thing that you need to do is get the files you need. Here is the Swin Messenger Library.zip or visit http://code.google.com/p/swinmessenger/downloads/list to check that it is the latest version. Once you have downloaded it extract all the files in Swin Messenger Library.zip and save to some were you will remember it.

Setting up the Project

Now you need to create a new project and add SwinMessengerLibary.dll as a reference, follow these steps:

  1. Create a new C# console application project in Visual Studio
  2. Add the SwinMessengerLibary.dll as a reference in the project, to add a references:
    1. Go over the Solution Explorer Window
    2. Expand the Project tree out until you see "References"
    3. Right click on "References" and select "Add Reference..."
    4. Select the "Browse" tab and now navigate to were you saved SwinMessengerLibary.dll
    5. Once you have found it click on "OK"

Setting up the first class

Now we need to add some code to "Program.cs"

  1. Open up Program.cs
  2. Add this top the top of the class
    1. using SwinMessengerLibrary;

Creating a server

Now we need to create a server so we can log into it

  1. Add the following code to the Program.cs class in the Main method
    1. private Server server = new Server("server", "account", "password");
    • "server" is the server that you wish to login to
    • "account" is the account that you have on the server
    • "password" is the password for the account
  2. Now that the server is created we can log into it. To log into a server write this
    1. server.Login();

Sending your first message

Now that you have logged into the server, you will probably want to send a message.

  1. To send a message you need to type this
    1. server.SentTo("Contact ID", "message");
    • "Contact ID" is the id of the contact you wish to send a message to
    • "message" is the message you want to send to them
    • The contact must be added to your roster for you to able send a message

Receiving your first message

What is the point of being able to send messages if you can't receive any messages. To receive message you need to do this

  1. You need to add this to Main
    1. foreach (Contact contact in server.Roster.Contacts)
    2. {
    3. contact.OnMessage += new EventHandler(contact_OnMessage);
    4. }
    • This must only be called once or else you will see the same message more than once
  2. Now we need to create the "contact_OnMessage" event. This will need to look like this:
    1. private void contact_OnMessage(object sender, EventArgs e)
    2. {
    3. if (e is MessageEventArgs && e != null)
    4. {
    5. if (((MessageEventArgs)e).Message != null)
    6. {
    7. System.Console.WriteLine("Message from: "+((MessageEventArgs)e).Id);
    8. System.Console.WriteLine("- "+((MessageEventArgs)e).Message);
    9. }
    10. }
    11. }
    • This will write the any messages you receive to the screen for you to read.

Shutting down the server

Once you have sent and received all the messages you want to, you need to log out of the server. To log out of the serve you need to do this:

  1. Add this to the end of Main
    1. server.LogOut(true);

Links

  1. Swin Messenger
  2. Swin Messenger:Hello World How To