Question

I have some code for XMPP messaging in C#. I am not able to send a message to another user.

using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.Collections;
using agsXMPP.protocol.iq.roster;
using System.Threading;
using Microsoft.Win32;

public partial class Talk : Form
{
   agsXMPP.XmppClientConnection objXmpp;

    public Talk()
    {
        InitializeComponent();
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        XmppClientConnection xmpp = new XmppClientConnection();
        xmpp.Server = "gmail.com";
        xmpp.ConnectServer = "talk.google.com";
        xmpp.Port = 5222;
        xmpp.Username = "Sender@gmail.com";
        xmpp.Password = "******";
        xmpp.Open();

        agsXMPP.Jid JID = new Jid("receiver@gmail.com");

        xmpp.MesagageGrabber.Add(JID, new agsXMPP.Collections.BareJidComparer(), new MessageCB(MessageCallBack), null);

        agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
        msg.Type = agsXMPP.protocol.client.MessageType.chat;
        msg.To = JID;
        msg.Body = " asdfasdfasdf " ;// simple string 

        xmpp.OnLogin += delegate(object o) { xmpp.Send(msg); };

        xmpp.Close();


    }
    static void MessageCallBack(object sender,agsXMPP.protocol.client.Message msg,object data)
    {
        if (msg.Body != null)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("{0}>> {1}", msg.From.User, msg.Body);
            Console.ForegroundColor = ConsoleColor.Green;
        }
    }
}

What am I doing wrong?

No correct solution

OTHER TIPS

1) username is sender (lowercase), not Sender@gmail.com. sender@gmail.com is the full Jid

2) agsXMPP is completly asynchronous. Which means your xmpp.Open() call does not block. Your runs runs through and closes the connection immediately before it was established.

3) look at the agsXMPP examples

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top