Sunday, August 29, 2010

Creating a GTalk client with C# - Part 2

I think you have already read my previous blog post : Creating a GTalk client with C# - Part 1. So now you have created a login form and only authenticated people can log into the client. in that post when a user authenticated what i did was just disposed the current window so in this post i will create another form and show it instead of disposing the login form. So create another form with VS where you should have a list to load you buddy list, another list to show your chats, a text box to type your chats and a send button.(This is my basic design but it is up to you to design whatever way you like :) )

alright everything is set to do the codings.... go to source and initialize a XmppClientConnection object but this object should be the same object as we created in the previous form. so what i did was made that earlier object static(in form 1) and also create a static method called getXmpp() in form1.cs


static XmppClientConnection xmppCon = new XmppClientConnection();




        public static XmppClientConnection getXmpp()
        {
            return xmppCon;
        }

you may find a better way than this. 

then in form2 add this line.



XmppClientConnection xmppCon = Form1.getXmpp();
now you have to subscribe to events so create a new method called init()


        private void Init()
        {
            chatList.Items.Clear();
            xmppCon.OnRosterStart += new ObjectHandler(OnRosterStart);
            xmppCon.OnRosterEnd += new ObjectHandler(OnRosterEnd);
            xmppCon.OnRosterItem += new XmppClientConnection.RosterHandler(OnRosterItem);
            xmppCon.OnPresence += new agsXMPP.protocol.client.PresenceHandler(OnPresence);
            xmppCon.OnError += new ErrorHandler(OnError);
            xmppCon.OnClose += new ObjectHandler(OnClose);
            xmppCon.OnMessage += new agsXMPP.protocol.client.MessageHandler(OnMessage);
        }



 here chatList is the ListBox which we create to show the chats and Roster means the buddies. now invoke this init method in the constructor after InitializeComponent(); 


        public Form2()
        {
            InitializeComponent();
            Init();
        }
now simply create the methods to handle the events which were registered.


       void OnMessage(object sender, agsXMPP.protocol.client.Message msg)
        {
            if (msg.Body == null)
                return;

            if (InvokeRequired)
            {
                BeginInvoke(new agsXMPP.protocol.client.MessageHandler(OnMessage), new object[] { sender, msg });
                return;
            }

            chatList.Items.Add(String.Format("{0} type:{1}", msg.From.Bare, msg.Type.ToString()));
            chatList.Items.Add(msg.Body);
            chatList.SelectedIndex = chatList.Items.Count - 1;          
        }

        void OnClose(object sender)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ObjectHandler(OnClose), new object[] { sender });
                return;
            };
        }

        void OnError(object sender, Exception ex)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ErrorHandler(OnError), new object[] { sender, ex });
                return;
            }
            chatList.Items.Add("Error");
            chatList.SelectedIndex = chatList.Items.Count - 1;
        }

       void OnPresence(object sender, agsXMPP.protocol.client.Presence pres)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new agsXMPP.protocol.client.PresenceHandler(OnPresence), new object[] { sender, pres });
                return;
            }
            chatList.Items.Add(String.Format("Received Presence from:{0} show:{1} status:{2}", pres.From.ToString(), pres.Show.ToString(), pres.Status));
            chatList.SelectedIndex = chatList.Items.Count - 1;
        }

        void OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new XmppClientConnection.RosterHandler(OnRosterItem), new object[] { sender, item });
                return;
            }
            buddyList.Items.Add(String.Format("{0}", item.Jid.Bare));
            buddyList.SelectedIndex = chatList.Items.Count - 1;
        }

        void OnRosterEnd(object sender)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ObjectHandler(OnRosterEnd), new object[] { sender });
                return;
            }
            xmppCon.SendMyPresence();
        }

        void OnRosterStart(object sender)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ObjectHandler(OnRosterStart), new object[] { sender });
                return;
            }
        }

this is simply the code you can customize in whatever way you like. :) so now the last thing in form1 we only dispose the form so now let's replace that line with this :



new Form2().Show();
 ok now run it. select a buddy from buddylist and chat... have fun... if you have any problem regarding the code feel free to comment below or send me an email to : kesh.jboy@gmail.com

cheers !
Kesh,