Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
pavenc #1
Member since Jun 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: Having trouble getting started
Hey, I'm fairly new to C# in general, and totally new to IM protocols, though I've been experimenting and researching for a few weeks now.  What I'd like to do is make a really *really* simple client that just has login and simple text send and receive (popup on receive would be ideal, but not necessary). 

I want to do as much of this as possible myself, but I've really been struggling just understanding the docs to figure out which classes and methods to work with.  Is there a working client available that uses the agsXMPP and has source code available to look at?

How far I've gotten is hard for me to assess.  I created a connection and attempted login.  Since I don't know what to do to receive and display messages yet, I don't know how to confirm that my login was successful. 

I don't know... If I'm too much of a raw noob, tell me to go elsewhere and I won't be offended.  I just was hoping to use this as a learning opportunity.

Thanks in advance,
Paul
Avatar
pecesama #2
Member since Jun 2005 · 7 posts
Group memberships: Members
Show profile · Link to this post
Hi pavenc,

  I am new in agsXMPP too  but i am working with the library and i have some code that may be useful for you, this is a really *really* simple client.


using System;
using System.Drawing;
using System.Windows.Forms;

using agsXMPP;
using agsXMPP.protocol;
using agsXMPP.protocol.iq;
using agsXMPP.protocol.iq.roster;
using agsXMPP.protocol.iq.agent;
using agsXMPP.Xml.DomParser;

using System.Threading;

namespace jabberin
{
    /// <summary>
    /// Really Simple Jabber Client
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label lblServer;
        private System.Windows.Forms.TextBox txtPass;
        private System.Windows.Forms.Label txtMsg;
        private System.Windows.Forms.Button btnSend;
        private System.Windows.Forms.GroupBox gbxXMPP;
        private System.Windows.Forms.TextBox txtDebug;
        private System.Windows.Forms.Label lblUser;
        private System.Windows.Forms.TextBox txtServer;
        private System.Windows.Forms.TextBox txtUser;
        private System.Windows.Forms.TextBox txtSend;
        private System.Windows.Forms.Label lblPass;
        private System.Windows.Forms.Button btnConnect;
        private System.Windows.Forms.Button btnDisconnect;
        private XmppClientConnection jClient; // Jabber Client Decalration
       
        public MainForm()
        {           
            InitializeComponent();           
        }
       
        [STAThread]
        public static void Main(string[] args)
        {
            Application.Run(new MainForm());
        }
       
        void BtnConnectClick(object sender, System.EventArgs e)
        {
            jClient = new XmppClientConnection(); // Jabber Client Instance

            /**
             * Events for agsXMPP library
             * */
            jClient.OnReadXml    += new agsXMPP.XmppClientConnection.XmlHandler(jClient_OnReadXml);
            jClient.OnWriteXml    += new agsXMPP.XmppClientConnection.XmlHandler(jClient_OnWriteXml);
            jClient.OnLogin    += new ObjectHandler(jClient_OnLogin);
            jClient.OnMessage += new agsXMPP.XmppClientConnection.MessageHandler(jClient_OnMessage);
            jClient.OnError    += new agsXMPP.XmppClientConnection.ErrorHandler(jClient_OnError);
           

            jClient.Server            = txtServer.Text;           
            jClient.Username        = txtUser.Text;
            jClient.Password        = txtPass.Text;
            jClient.Port            = 5222;
            jClient.Resource        = &quot;jabberin&quot;;

            jClient.Priority        = 5;
            jClient.Status            = &quot;Online&quot;;
            jClient.Show            = ShowType.NONE;

            try
            {
                jClient.Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       
        void BtnSendClick(object sender, System.EventArgs e)
        {
            jClient.Send(new agsXMPP.protocol.Message(&quot;someJabberIdHere&quot;, agsXMPP.protocol.MessageType.chat, txtSend.Text));
        }
       
        void BtnDisconnectClick(object sender, System.EventArgs e)
        {
            try
            {
                jClient.Close();
                gbxXMPP.Enabled = false;
            }
            catch
            {
                Application.Exit();
            }
        }
       
        private void jClient_OnLogin(object sender)
        {
           
            txtDebug.Text += &quot;we are logged in to the server now&quot; + System.Environment.NewLine;
            txtDebug.Text += &quot;set presence&quot; + System.Environment.NewLine;
            txtDebug.SelectionStart = txtDebug.Text.Length;
            txtDebug.ScrollToCaret();
            jClient.SendMyPresence();
            gbxXMPP.Enabled = true;
        }

        private void jClient_OnError(object sender, Exception ex)
        {
            MessageBox.Show(ex.Message,&quot;Error&quot;,MessageBoxButtons.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
        }

        private void jClient_OnReadXml(object sender, string xml)
        {
            txtDebug.Text += &quot;RECV XML: &quot; + xml + System.Environment.NewLine;
            txtDebug.SelectionStart = txtDebug.Text.Length;
            txtDebug.ScrollToCaret();
        }

        private void jClient_OnWriteXml(object sender, string xml)
        {
            txtDebug.Text += &quot;SEND XML: &quot; + xml + System.Environment.NewLine;
            txtDebug.SelectionStart = txtDebug.Text.Length;           
            txtDebug.ScrollToCaret();
        }

        private void jClient_OnMessage(object sender, agsXMPP.protocol.Message msg)
        {
            MessageBox.Show(msg.Body,&quot;Message&quot;,MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
        }
       
        #region Windows Forms Designer generated code
        /// <summary>
        /// This method is required for Windows Forms designer support.
        /// Do not change the method contents inside the source code editor. The Forms designer might
        /// not be able to load this method if it was changed manually.
        /// </summary>
        private void InitializeComponent() {
            this.btnDisconnect = new System.Windows.Forms.Button();
            this.btnConnect = new System.Windows.Forms.Button();
            this.lblPass = new System.Windows.Forms.Label();
            this.txtSend = new System.Windows.Forms.TextBox();
            this.txtUser = new System.Windows.Forms.TextBox();
            this.txtServer = new System.Windows.Forms.TextBox();
            this.lblUser = new System.Windows.Forms.Label();
            this.txtDebug = new System.Windows.Forms.TextBox();
            this.gbxXMPP = new System.Windows.Forms.GroupBox();
            this.btnSend = new System.Windows.Forms.Button();
            this.txtMsg = new System.Windows.Forms.Label();
            this.txtPass = new System.Windows.Forms.TextBox();
            this.lblServer = new System.Windows.Forms.Label();
            this.gbxXMPP.SuspendLayout();
            this.SuspendLayout();
            //
            // btnDisconnect
            //
            this.btnDisconnect.Location = new System.Drawing.Point(104, 72);
            this.btnDisconnect.Name = &quot;btnDisconnect&quot;;
            this.btnDisconnect.TabIndex = 9;
            this.btnDisconnect.Text = &quot;Disconnect&quot;;
            this.btnDisconnect.Click += new System.EventHandler(this.BtnDisconnectClick);
            //
            // btnConnect
            //
            this.btnConnect.Location = new System.Drawing.Point(16, 72);
            this.btnConnect.Name = &quot;btnConnect&quot;;
            this.btnConnect.TabIndex = 8;
            this.btnConnect.Text = &quot;Connect&quot;;
            this.btnConnect.Click += new System.EventHandler(this.BtnConnectClick);
            //
            // lblPass
            //
            this.lblPass.AutoSize = true;
            this.lblPass.Location = new System.Drawing.Point(8, 32);
            this.lblPass.Name = &quot;lblPass&quot;;
            this.lblPass.Size = new System.Drawing.Size(33, 16);
            this.lblPass.TabIndex = 3;
            this.lblPass.Text = &quot;Pass:&quot;;
            //
            // txtSend
            //
            this.txtSend.Location = new System.Drawing.Point(50, 32);
            this.txtSend.Name = &quot;txtSend&quot;;
            this.txtSend.Size = new System.Drawing.Size(150, 20);
            this.txtSend.TabIndex = 0;
            this.txtSend.Text = &quot;&quot;;
            //
            // txtUser
            //
            this.txtUser.Location = new System.Drawing.Point(48, 8);
            this.txtUser.Name = &quot;txtUser&quot;;
            this.txtUser.Size = new System.Drawing.Size(104, 20);
            this.txtUser.TabIndex = 0;
            this.txtUser.Text = &quot;&quot;;
            //
            // txtServer
            //
            this.txtServer.Location = new System.Drawing.Point(184, 29);
            this.txtServer.Name = &quot;txtServer&quot;;
            this.txtServer.TabIndex = 7;
            this.txtServer.Text = &quot;&quot;;
            //
            // lblUser
            //
            this.lblUser.AutoSize = true;
            this.lblUser.Location = new System.Drawing.Point(8, 8);
            this.lblUser.Name = &quot;lblUser&quot;;
            this.lblUser.Size = new System.Drawing.Size(31, 16);
            this.lblUser.TabIndex = 2;
            this.lblUser.Text = &quot;User:&quot;;
            //
            // txtDebug
            //
            this.txtDebug.Location = new System.Drawing.Point(16, 64);
            this.txtDebug.Multiline = true;
            this.txtDebug.Name = &quot;txtDebug&quot;;
            this.txtDebug.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtDebug.Size = new System.Drawing.Size(248, 88);
            this.txtDebug.TabIndex = 2;
            this.txtDebug.Text = &quot;&quot;;
            //
            // gbxXMPP
            //
            this.gbxXMPP.Controls.Add(this.txtMsg);
            this.gbxXMPP.Controls.Add(this.txtDebug);
            this.gbxXMPP.Controls.Add(this.btnSend);
            this.gbxXMPP.Controls.Add(this.txtSend);
            this.gbxXMPP.Enabled = false;
            this.gbxXMPP.Location = new System.Drawing.Point(8, 104);
            this.gbxXMPP.Name = &quot;gbxXMPP&quot;;
            this.gbxXMPP.Size = new System.Drawing.Size(280, 160);
            this.gbxXMPP.TabIndex = 10;
            this.gbxXMPP.TabStop = false;
            this.gbxXMPP.Text = &quot;XMPP&quot;;
            //
            // btnSend
            //
            this.btnSend.Location = new System.Drawing.Point(208, 32);
            this.btnSend.Name = &quot;btnSend&quot;;
            this.btnSend.Size = new System.Drawing.Size(56, 23);
            this.btnSend.TabIndex = 1;
            this.btnSend.Text = &quot;Send&quot;;
            this.btnSend.Click += new System.EventHandler(this.BtnSendClick);
            //
            // txtMsg
            //
            this.txtMsg.AutoSize = true;
            this.txtMsg.Location = new System.Drawing.Point(16, 34);
            this.txtMsg.Name = &quot;txtMsg&quot;;
            this.txtMsg.Size = new System.Drawing.Size(29, 16);
            this.txtMsg.TabIndex = 3;
            this.txtMsg.Text = &quot;Msg:&quot;;
            //
            // txtPass
            //
            this.txtPass.Location = new System.Drawing.Point(48, 32);
            this.txtPass.Name = &quot;txtPass&quot;;
            this.txtPass.PasswordChar = '*';
            this.txtPass.Size = new System.Drawing.Size(104, 20);
            this.txtPass.TabIndex = 1;
            this.txtPass.Text = &quot;&quot;;
            //
            // lblServer
            //
            this.lblServer.AutoSize = true;
            this.lblServer.Location = new System.Drawing.Point(160, 8);
            this.lblServer.Name = &quot;lblServer&quot;;
            this.lblServer.Size = new System.Drawing.Size(41, 16);
            this.lblServer.TabIndex = 6;
            this.lblServer.Text = &quot;Server:&quot;;
            //
            // fmrMain
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.gbxXMPP);
            this.Controls.Add(this.btnDisconnect);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.lblServer);
            this.Controls.Add(this.txtServer);
            this.Controls.Add(this.lblPass);
            this.Controls.Add(this.lblUser);
            this.Controls.Add(this.txtPass);
            this.Controls.Add(this.txtUser);
            this.Name = &quot;fmrMain&quot;;
            this.Text = &quot;Jabberin&quot;;
            this.gbxXMPP.ResumeLayout(false);
            this.ResumeLayout(false);
        }
        #endregion
           
    }
}
Avatar
pavenc #3
Member since Jun 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Thanks!  That looks like something I can start picking apart and figuring out.
Avatar
pavenc #4
Member since Jun 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
So, I tried that and played around.  So far I was able to establish a good connection, but when I tried sending a message to myself (since I don't know anyone else on Jabber yet) it, I got an error which closed the connection:

SEND XML: <message xmlns="jabber:client" to="pavenc@jabber.org" type="chat"><body>Test message</body></message>
RECV XML: <stream:stream:error xmlns:stream="http://etherx.jabber.org/streams">Connection is closing</stream:stream:error>
RECV XML: </stream>


That was using your above listed code, all I did was add a place to put the recipient's info.  Thoughts?
Avatar
pavenc #5
Member since Jun 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Note: I got the same error when I deployed this to a friend and he logged in as himself and tried to send me a message.  Rather, my friend got it.
Avatar
Alex #6
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
hi pavenc,

did you create the message with agsXMPP message class, or did you just create the string yourself? Could you paste the code? For me it looks OK, and i see no reason why the server is closing the connection. But i also never tried to send messages to myself.
Just create another account on a  public server of your choice. Login with a your prefered jabber client and send the messages from one account to the other. Its also very easy to setup a own server for testing on your local machine.
There are Windows versions of jabberd 1.x and 2.x, ejabberd, jive messenger and soapbox.

Alex
Avatar
pavenc #7
Member since Jun 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Alex -- thanks for taking the time to write me.  Yeah, the connection code is as above (I'm still in the 'try to understand' phase all I modified on his code was a way to tell the client who you're sending to), the send message code is called in the btnSendClick event as:

jClient.Send(new agsXMPP.protocol.Message("pavenc@jabber.org", agsXMPP.protocol.MessageType.chat, strMessage));

strMessage is previously set as:

strMessage = txtSend.Text;

where txtSend is obviously a text box on the form to write the message you want to send.

Thanks again for any help you come up with.
Avatar
Alex #8
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
which server are you using for your tests?
Could you send me your sample code by email? Then i can take a closer look.

Alex
Avatar
pecesama #9
Member since Jun 2005 · 7 posts
Group memberships: Members
Show profile · Link to this post
Pavenc i sent messages to myself with the above code and the server doesn't close the connection.

Maybe you can try with the solution example is on the next url, you need #develop to open or include the MainForm.cs on VS.Net

http://www.pecesama.net/opensource/jabberin.zip
Avatar
pavenc #10
Member since Jun 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Thanks, fellas, I'll try the link you sent.  Not sure what you mean by #develop (I'm new to C#, remember?)?

Anyway, I'll post later w/ success or failure off that zip.

Paul
Avatar
Alex #11
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
SharpDevelop (#develop) is a is a free IDE for C# and VB.NET projects on Microsoft's and MONO's .NET platform.
http://www.icsharpcode.net/OpenSource/SD/Default.aspx
Avatar
jvallieres #12
Member since Jun 2005 · 1 post
Group memberships: Members
Show profile · Link to this post
Subject: Question
I have tried running all the code in this post, and at least one thread is never being released.  After the app has closed it still remains as a running process.  I have figured out a really ugly way of killing the thread, but I don't think aborting the thread is really the way to go.  Has anyone else experienced this?  Any ideas?
Avatar
Alex #13
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
hi,

i had never problems like this. Its possible that the xml stream gets never closed correct. Because the parser runs in it own thread. You could also contact me by mail and i will send you the latest developer version.

Alex
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:
Forum: agsXMPP RSS