Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
Victor #1
User title: Victor
Member since Nov 2006 · 5 posts
Group memberships: Members
Show profile · Link to this post
Subject: Presence Detection problem
I'm having trouble with presence detection. The Presence handler only seems to be called when another user subscribes to me.
It is never called when a user logs in or out. when I logon with the code below then the presence detection in the Miniclient and other clients works OK.
 I'm running Wildfire on my machine as the server.
Any ideas?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using agsXMPP;
using agsXMPP.Collections;
using System.Threading;


namespace Manpreets
{
    public partial class frmMain : Form
    {
        private XmppClientConnection m_Connection;
        private EventLog m_Log;
        private EventLog m_SocketLog;

        private Jid m_JidTo;

        public delegate void EventMsgDelegate(string p_Msg);


        [STAThread]
        static void Main()
        {
            Application.Run(new frmMain());
        }

        public frmMain()
        {
            InitializeComponent();
            this.m_Log = new EventLog(this.txtLog);
            this.m_SocketLog = new EventLog(this.rtfDebugSocket);
        }

        private void btnSendToVictor_Click(object sender, EventArgs e)
        {
            this.m_JidTo = new Jid("VictorFuller", "fullerv", "MiniClient");

            this.SendMessage(this.txtMessageToSend.Text);
        }

        private void btnSendToSpot_Click(object sender, EventArgs e)
        {
            this.m_JidTo = new Jid("Spot", "fullerv", "MiniClient");

            this.SendMessage(this.txtMessageToSend.Text);

        }


        private void frmMain_Load(object sender, EventArgs e)
        {
 


        }
        public void XmppOnReceiveXml(object sender, string xml)
        {
            //((XmppGui)this.GuiInterface).SetDebugLabel(xml);
            System.Console.WriteLine("XMPP: " + xml);
        }

        private void OnRosterStart(object sender)
        {
            this.m_Log.AddEvent("OnRosterStart raised");
        }

        private void OnRosterEnd(object sender)
        {
            this.m_Log.AddEvent("OnRosterEnd raised");
        }

        private void OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {

            this.m_Log.AddEvent("OnRosterItem raised JID:" + item.Jid);
        }

        private void OnMyLogin(object sender)
        {
            this.m_Log.AddEvent("OnLogin rasied");
            m_Connection.Show = agsXMPP.protocol.client.ShowType.chat;
            m_Connection.Status = "Online";

            m_Connection.SendMyPresence();
            this.m_Log.AddEvent("SendMyPresence() called");
            // Setup new Message Callback


            m_Connection.MesagageGrabber.Add(new Jid("Spot", "fullerv", "MiniClient"), new BareJidComparer(), new MessageCB(MessageCallback), null);
            m_Connection.MesagageGrabber.Add(new Jid("VictorFuller", "fullerv", "MiniClient"), new BareJidComparer(), new MessageCB(MessageCallback), null);
        }



    private void MessageCallback(object sender, agsXMPP.protocol.client.Message msg, object data)
    {
            if (msg.Body != null)
            {
                if (msg.Body.Length==0)
                    this.AddEvent("Empty string received");
                else 
                  this.AddEvent("Received: " + msg.Body);
            }
            else
                this.AddEvent("Null message recieved");
    }

        public void AddEvent(string p_Message)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke               
                BeginInvoke(new EventMsgDelegate(AddEvent), new object[] { p_Message });
                return;
            }


            this.m_Log.AddEvent(p_Message);
        }



        public void OnError(object sender, Exception ex)
        {
            MessageBox.Show(ex.StackTrace);
        }

        public void SendMessage(string p_Msg)
        {

            agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message(this.m_JidTo, p_Msg);
            msg.Type = agsXMPP.protocol.client.MessageType.chat;
            m_Connection.Send(msg);
            this.m_Log.AddEvent("Send message to " + m_JidTo.User);
        }

        private void btnOnlineBetFair_Click(object sender, EventArgs e)
        {
            this.m_Log.AddEvent("Online BetFAir@fullerv");
            this.PutUserOnline("BetFair", "fullerv", "Postage271");

        }


        private void PutUserOnline(string p_UserName,string p_DomainName,string p_Password)
        {
            CheckForIllegalCrossThreadCalls = false;
            //random = new Random();

            // register element
            //  agsXMPP.Factory.ElementFactory.AddElementType("alert", "betfairatg:alert", typeof(Alert));

            // Create Connection
            m_Connection = new XmppClientConnection();
            m_Connection.SocketConnectionType = agsXMPP.net.SocketConnectionType.Direct;
            m_Connection.Server = p_DomainName;
            m_Connection.Port = 5222;
            m_Connection.Username = p_UserName;
            m_Connection.Password = p_Password;
            m_Connection.Resource = "MiniClient";
            m_Connection.Priority = 10;
            m_Connection.UseSSL = false;
            m_Connection.AutoResolveConnectServer = true;
            m_Connection.AutoRoster = true;

            m_Connection.OnLogin += new ObjectHandler(OnMyLogin);
            m_Connection.OnError += new ErrorHandler(OnError);
            m_Connection.OnReadXml += new XmlHandler(XmppOnReceiveXml);
            m_Connection.OnPresence += new XmppClientConnection.PresenceHandler(OnPresenceDetected);
            m_Connection.OnXmppConnectionStateChanged += new agsXMPP.XmppClientConnection.XmppConnectionStateHandler(XmppCon_OnXmppConnectionStateChanged);
            m_Connection.OnReadSocketData += new agsXMPP.net.BaseSocket.OnSocketDataHandler(ClientSocket_OnReceive);
            m_Connection.OnWriteSocketData += new agsXMPP.net.BaseSocket.OnSocketDataHandler(ClientSocket_OnSend);
            m_Connection.OnIq += new agsXMPP.Xml.StreamHandler(OnIq);
            m_Connection.OnRosterStart += new ObjectHandler(OnRosterStart);
            m_Connection.OnRosterEnd += new ObjectHandler(OnRosterEnd);
            m_Connection.OnRosterItem += new XmppClientConnection.RosterHandler(OnRosterItem);


            m_Connection.Open();

        }

        private void OnPresenceDetected(object sender, agsXMPP.protocol.client.Presence pres)
        {
            this.AddEvent("Presence Detected: " + pres.From.User + "@" + pres.From.Server + "/" + pres.From.Resource + " Type " + pres.Type);

            switch(pres.Type)
            {
                case agsXMPP.protocol.client.PresenceType.subscribe:
                    agsXMPP.protocol.client.PresenceManager pm = new agsXMPP.protocol.client.PresenceManager(m_Connection);
                      pm.ApproveSubscriptionRequest(pres.From);
                      break;
                case agsXMPP.protocol.client.PresenceType.available:
                    this.AddEvent(pres.From.User + "@" + pres.From.Server + "/" + pres.From.Resource + " is Online");
                        break;
                case agsXMPP.protocol.client.PresenceType.unavailable:
                    this.AddEvent(pres.From.User + "@" + pres.From.Server + "/" + pres.From.Resource + " is Offline-unavailable");
                        break;

                default:
                    break;



            }
        }
  
        private void XmppCon_OnXmppConnectionStateChanged(object sender, XmppConnectionState state)
        {
            this.AddEvent("Connection State Changed: " + state.ToString());
        }

        private void ClientSocket_OnReceive(object sender, byte[] data, int count)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke               
                BeginInvoke(new agsXMPP.net.ClientSocket.OnSocketDataHandler(ClientSocket_OnReceive), new object[] { sender, data, count });
                return;
            }

            try
            {
                this.m_SocketLog.TextColour = Color.Red;
                 this.m_SocketLog.AddFirstPartOfEvent("RECV: ");

                 this.m_SocketLog.TextColour = Color.Black;
                this.m_SocketLog.AddEvent(System.Text.Encoding.Default.GetString(data, 0, count));


            }
            catch { }
        }

        private void ClientSocket_OnSend(object sender, byte[] data, int count)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke               
                BeginInvoke(new agsXMPP.net.ClientSocket.OnSocketDataHandler(ClientSocket_OnSend), new object[] { sender, data, count });
                return;
            }

            try
            {
                this.m_SocketLog.TextColour = Color.Blue;
                this.m_SocketLog.AddFirstPartOfEvent("SEND:  ");

                this.m_SocketLog.TextColour = Color.Black;
                this.m_SocketLog.AddEvent(System.Text.Encoding.Default.GetString(data, 0, count));

            }
            catch { }
        }

        private void OnIq(object sender, agsXMPP.Xml.Dom.Node e)
        {
            agsXMPP.protocol.client.IQ iq = e as agsXMPP.protocol.client.IQ;
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello Victor,

the code is not complete. Can you zip your code and attach it to the post? If you expand the post options you can add attachments.

Alex
Avatar
Victor #3
User title: Victor
Member since Nov 2006 · 5 posts
Group memberships: Members
Show profile · Link to this post
Alex
a zip of the whole project is attached, thanks for your help.
The author has attached one file to this post:
agsXMPPTester.zip 12.9 kBytes
You have no permission to open this file.
Avatar
Alex #4
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello Victor,

your sample works great here. I have no problems at all with it and get all presences in your presence handler.
Are you sure the subscription of your test accounts is OK? Because without subscription to a contact you never will get it's presence from the server.

Alex
Avatar
Victor #5
User title: Victor
Member since Nov 2006 · 5 posts
Group memberships: Members
Show profile · Link to this post
I think the subscription is OK. I've been using the Miniclient to run a test account. The Miniclient detects the presence of my client OK, both at logon and logoff. Does this imply subscription is OK?
My client however never rasies the OnPresence handler at logon or logoff only for a Subscription.
Avatar
Alex #6
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello Victor,

the presence is the online state of a contact. And in XMPP you only are allowed to the the online state of contacts you have subscription to.
I would suggest that you download a XMPP client [1] and test them with 2 test accounts. Exodus is a very good clients for developers. If you get the presences there and the subscription is ok, then you also get them in your code.

Alex

[1] http://www.jabber.org/software/clients.shtml
Avatar
Victor #7
User title: Victor
Member since Nov 2006 · 5 posts
Group memberships: Members
Show profile · Link to this post
I’ve been testing with several clients and the MiniClient. They all detect presence between each other. They all detect my client’s presence. My Client does not detect logon/logout presence from any of them.
When I unsubscribe from the user running on my client I do see an unsubscribe presence message. When I then subscribe, using the same user, to the user on my client I do not see the subscribe presence. If I create a new user and subscribe to the user on my client I DO see the subscribe message.

Everything, all clients and the server are running on my local machine. I’m running WildFire 3.1.1.
Avatar
Alex #8
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello Victor,

thats strange because your attached sample works for me without changes.
Why not take the MiniClient as a base if it works for you?

Alex
Avatar
Victor #9
User title: Victor
Member since Nov 2006 · 5 posts
Group memberships: Members
Show profile · Link to this post
Yeah, it's very strange indeed that the MiniClient works here, my code works with you but my code dosen't work here!

I'll examine the MiniClient and make a closer copy of the code in my client. I guess there's something I've missed out that's making things go wrong.

Thanks for you help. If anything else does occur to you let me know.

    Victor
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