无法使用c#和agsXMPP将用户添加到ejabbered

时间:2015-08-31 06:55:29

标签: agsxmpp

我正在iOS中开发一个聊天应用程序,而API则在Windows服务器上。已经在服务器上安装了ejabbered并且能够使用agsXMPP从C#成功连接。

问题在于我无法每次注册用户,有时会成功执行,有时则无法执行。以下是代码:

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

    }

注册失败时我会遇到不同的错误:

  • agsXMPP.Xml.Xpnet.InvalidTokenException
  • 无法投射类型的对象 输入'agsXMPP.protocol.stream.feature.Register' 'agsXMPP.protocol.Stream'
  • 无法从传输中读取数据 connection:无法访问已处置的对象。\ r \ n对象名称: 'System.Net.Sockets.Socket'

尽管代码没有变化,但却得到了不同的错误

0 个答案:

没有答案