Not able to add user to ejabbered using c# and agsXMPP

406 Views Asked by At

I am developing a chat application in iOS and the APIs are on Windows server. Have got ejabbered installed on the server and am able to connect successfully from C# using agsXMPP.

The issue is that I am not able to register user every time, it sometimes executes successfully and sometimes not. Below is the code:

 static ManualResetEvent done = new ManualResetEvent(false);
    public void Login()
    {
        XmppClientConnection xmpp = (XmppClientConnection)Application["xmpp"];
        try
        {
            if (xmpp == null)
            {
                xmpp = new XmppClientConnection();
                Application["xmpp"] = xmpp;
            }

            xmpp.OnLogin += new ObjectHandler(xmpp_OnLogin);
            xmpp.OnError += new ErrorHandler(xmpp_OnError);
            xmpp.AutoPresence = true;

            xmpp.AutoResolveConnectServer = false;
            xmpp.Port = 5222;
            xmpp.UseSSL = false;
            xmpp.ConnectServer = "127.0.0.1";
            xmpp.Server = "plutus-pc1";
            xmpp.Username = "admin";
            xmpp.Password = "p@ssword";
            xmpp.KeepAlive = true;

            xmpp.Open();

            done.WaitOne();
            Register();


        }
        catch (Exception ex)
        {
            xmpp.Close();
        }


    }

    void xmpp_OnError(object sender, Exception ex)
    {
        XmppClientConnection xmpp = (XmppClientConnection)Application["xmpp"];
        xmpp.Close();
    }

    void xmpp_OnLogin(object sender)
    {
        done.Set();
    }

    protected void cmdLogout_Click(object sender, EventArgs e)
    {
        XmppClientConnection xmpp = (XmppClientConnection)Application["xmpp"];
        xmpp.Close();
    }

    public void Register()
    {
        try
        {
            XmppClientConnection xmpp;
            xmpp = (XmppClientConnection)Application["xmpp"];
            if (xmpp == null)
            {
                xmpp = new XmppClientConnection();
                Application["xmpp"] = xmpp;
            }


            xmpp.AutoPresence = true;

            xmpp.AutoResolveConnectServer = false;
            xmpp.Port = 5222;
             xmpp.UseSSL = false;
            xmpp.ConnectServer = "127.0.0.1";
            xmpp.Server = "plutus-pc1";
            xmpp.Username = "user1";
            xmpp.Password = "p@ssword1";
             xmpp.RegisterAccount = true;

            xmpp.OnRegisterError += xmpp_OnRegisterError;
            xmpp.OnRegistered += xmpp_OnRegistered;
            xmpp.Open();


            //done.WaitOne();
        }
        catch (Exception ex)
        {
        }


    }

    void xmpp_OnRegistered(object sender)
    {

    }


    void xmpp_OnRegisterError(object sender, agsXMPP.Xml.Dom.Element e)
    {
        //throw new NotImplementedException();

    }

I get different errors when registration is not successful:

  • agsXMPP.Xml.Xpnet.InvalidTokenException
  • Unable to cast object of type 'agsXMPP.protocol.stream.feature.Register' to type 'agsXMPP.protocol.Stream'
  • Unable to read data from the transport connection: Cannot access a disposed object.\r\nObject name: 'System.Net.Sockets.Socket'

Despite not change in code, get different errors

0

There are 0 best solutions below