Friday, August 27, 2010

Creating a GTalk client with C# - Part 1

Today i am going to create a GTalk client with C# on Visual Studio 2008. So without talking much let's start developing :). here i am using agsXMPP library. so first you need to download and install agsXMPP SDK. open Visual Studio and create a new project.


first of all we need to create some kind of a login form so only authenticated person can log in. design a login form with VS.


ok design has been done ! now go to the source of the form and initialize a XmppClientConnection object
XmppClientConnection xmppCon = new XmppClientConnection();
in this first part i am only going to implement the authentication part and sending and receiving chats will be covered in the second part and i hope to add some new and cool speech technologies in third part :).
so for authentication and login we have to declare handlers in the constructor 
xmppCon.OnLogin += new ObjectHandler(OnLogin);
xmppCon.OnAuthError += new XmppElementHandler(OnAuthError);
xmppCon.OnError += new ErrorHandler(OnError);
so, this means we should have three methods  OnLogin, OnAuthError and OnError

void OnError(object sender, Exception ex)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ErrorHandler(OnError), new object[] { sender, ex });
                return;
            }
            MessageBox.Show("Error occurred");
        }

this method invokes when an error occurred so, you can handle it anyway you like. since windows forms are not thread safe here and in next two methods also i have to invoke the BeginInvoke method.
void OnAuthError(object sender, agsXMPP.Xml.Dom.Element e)

        {
            if (InvokeRequired)
            {
                BeginInvoke(new XmppElementHandler(OnAuthError), new object[] { sender, e });
                return;
            }
            MessageBox.Show("Your username or password in invalid");
        }


void OnLogin(object sender)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ObjectHandler(OnLogin), new object[] { sender });
                return;
            }
            this.Dispose();           
        }
here in OnLogin method i did nothing but disposing the current form but in my next post i will explain how to create another form and load our buddy list and send receive chats with them.
so are we done with this? :) this code won't compile because we didn't add any reference to agsXMPP library so right click on References and browse to the location where you installed add reference to agsxmpp.dll then add

using agsXMPP;

this will compile and run but nothing will happen because we still didn't say anything to do when we click the login button so now it is time to do it. double click on the button so VS automatically generate the method.

            Jid jidUser = new Jid(username.Text);

            xmppCon.Username = jidUser.User;
            xmppCon.Server = jidUser.Server;
            xmppCon.Password = password.Text;
            xmppCon.AutoResolveConnectServer = true;

            xmppCon.Open();

now this is it. done! the form will be disposed if you are successfully logged in my next part i will explain how to chat not only disposing the current form.. if you have any question feel free to comment below.

Cheers !
Kesh,