Not logged in. · Lost password · Register
Forum: XMPP Protocol RSS
Avatar
pam #1
Member since Feb 2008 · 3 posts
Group memberships: Members
Show profile · Link to this post
Subject: agsXMPP
i am developing a Windows application in c# in which i am required to connect to the Openfire server via a chat client which i am tryn to develop in csharp
the code i have written uptil now is as :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Text;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.Collections;
using agsXMPP.protocol.iq.roster;
using System.Threading;


namespace JClient
{
    public partial class JClient : Form
    {
        static bool _wait;
        static string JID_Sender;
        static string password;
        static Jid jidSender = new Jid(JID_Sender);
        string JID_Receiver,outMessage;
        XmppClientConnection xmpp = new XmppClientConnection(jidSender.Server);       

        public JClient()
        {
            InitializeComponent();           
         
        }

        private void login_Click(object sender, EventArgs e)
        {
            JID_Sender = jid.Text;
            password = pword.Text;
            MessageBox.Show("The jid is:" + JID_Sender);
            MessageBox.Show("The password is:" + password);

            Jid jidSender = new Jid(JID_Sender);
            XmppClientConnection xmpp = new XmppClientConnection(jidSender.Server);
            try
            {
                xmpp.Open(jidSender.User, password);
                xmpp.OnLogin += new ObjectHandler(xmpp_OnLogin);
            }
            catch (Exception k)
            {
                MessageBox.Show(k.Message);
            }
            MessageBox.Show("waiting for login...1");
            info.Text = "Wait for Login ";
            MessageBox.Show("waiting for login...2");
            int i = 0;
            _wait = true;
            do
            {
                info.Text += ".";
                i++;
                if (i == 10)
                    _wait = false;
                Thread.Sleep(500);
            } while (_wait);
            info.Clear();
            MessageBox.Show("waiting for login...3");
            info.Text += "Login Status:";
            info.Text += "xmpp Connection State {0}" + xmpp.XmppConnectionState;
            info.Text += "xmpp Authenticated? {0}" + xmpp.Authenticated;
            MessageBox.Show("waiting for login...4");
            info.Clear();
            // }


            MessageBox.Show("before presence!");
            info.Text = "Sending Precence";
            Presence p = new Presence(ShowType.chat, "Online");
            p.Type = PresenceType.available;
            xmpp.Send(p);
            MessageBox.Show("after presence!");

            xmpp.OnPresence += new PresenceHandler(xmpp_OnPresence);

            Thread.Sleep(500);
            info.Text = "";
           MessageBox.Show("Enter Chat Partner JID:");
            i = 0;
            _wait = true;
            do
            {
                //info.Text += ".";
                i++;
                if (i == 10)
                    _wait = false;
                Thread.Sleep(500);
            } while (_wait);
            //MessageBox.Show("after chat partner!");
            info.Text = "";

            info.Text = "Start Chat";
            info.Text = "";
        }


        private void button1_Click(object sender, EventArgs e)
        {

            JID_Receiver = info.Text;
            //MessageBox.Show("partners JID: " + JID_Receiver);
            info.Clear();
            MessageBox.Show("START CHAT!");
            //goto l1;
        }

     
      
       
    


        static void xmpp_OnPresence(object sender, Presence pres)
        {
            MessageBox.Show("Available Contacts: ");
            MessageBox.Show("{0}@{1}  {2}" + pres.From.User + pres.From.Server + pres.Type);
            //jcStatus.Text=" ";(pres.From.User + "@" + pres.From.Server + "  " + pres.Type);
            //   jcStatus.Text=" ";();
        }

        // Is raised when login and authentication is finished
        static void xmpp_OnLogin(object sender)
        {
            _wait = false;
            MessageBox.Show("Logged In");
            //info.Text += "Logged in";
        }

        //Handles incoming messages


        static void MessageCallBack(object sender,
                                   agsXMPP.protocol.client.Message msg,
                                   object data)
        {
            MessageBox.Show("BUZZZZZ...");
            if (msg.Body != null)
            {
                MessageBox.Show("WE R IN!");
                // Console.ForegroundColor = ConsoleColor.Red;
                //jcReceive.Text=
             MessageBox.Show("{0}>> {1}" + msg.From.User + msg.Body);       ////////////chk this it doesnt recognize text box name here
                // Console.ForegroundColor = ConsoleColor.Green;
            }
        }

        private void jcSend_KeyDown(object sender, KeyEventArgs e)
        {
            bool halt = false;
            outMessage = jcSend.Text;

            if (e.KeyCode == Keys.Enter)
            {


                xmpp.MesagageGrabber.Add(new Jid(JID_Receiver),
                                new BareJidComparer(),
                                new MessageCB(MessageCallBack),
                                null);

                MessageBox.Show("jid of partner: " + JID_Receiver);
                // bool halt = false;
                //do
                //{
                    // Console.ForegroundColor = ConsoleColor.Green;
                //    outMessage = jcSend.Text;
                //    MessageBox.Show("jcSendText: " + jcSend.Text);
                    if (outMessage == "q!")
                    {
                        //halt = true;
                        xmpp.Close();
                    }
                    else
                    {
                        xmpp.Send(new agsXMPP.protocol.client.Message(new Jid(JID_Receiver),
                                      MessageType.chat,
                                      outMessage));
                        MessageBox.Show("Message sent");
                    }
             //   } while (!halt);

            }


            //xmpp.Close();
        }

     
        

    }

}

       
   
In this while debugging control isint reaching inside the callback function. After pressing the enter key the msg is being sent but not being received by the other client
Login and authentication is working properly.
The Windows form consists of 4 text-boxes for id(jid),password(pword),input (jcSend)and output (info). I have used two buttons: 1 for login (login)and 1 for taking the partners jid from the user ( button1).
 Can u plz help?
Avatar
Alex #2
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
It does not help when you post the same questions in different threads.

Please start with the MiniClient example which ships with the SDK in the example directory. Its a basic and complete chat client. If this example also does not work I assume there is a problem with your server setup.
Avatar
pam #3
Member since Feb 2008 · 3 posts
Group memberships: Members
Show profile · Link to this post
hi
thanks for replying
I have just started using XMPP and the MiniClient ex is a bit Too complicated. Could u plz indicate where exactly i am going wrong in this code? I need it for the college project. The callback function doesnt seem to be working. The message is being sent but not being received by the other client.
We are connecting to the Openfire server..
could u help me out with this code plz?
Avatar
Alex #4
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
why are you sing the loops in a windows forms based application which is event driven?
Use invokes like in the MiniClient example for your events which update the UI because Winforms are not thread safe.

Alex
Avatar
pam #5
Member since Feb 2008 · 3 posts
Group memberships: Members
Show profile · Link to this post
thanks...i got it working :)
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters: