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;
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;