使用Jabber-Net连接到Openfire服务器时错误绑定资源 [英] Error Binding Resource while Connecting to Openfire server using Jabber-Net

查看:103
本文介绍了使用Jabber-Net连接到Openfire服务器时错误绑定资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找C#xmpp库,因为我现在不得不排除使用Soapbox Studio SDK和aggXmpp/Matrix SDK的麻烦.我进入了 Jabber-net 剩下的几个可用的C#库之一,并且到目前为止,无法连接到我的Openfire服务器.从我了解到的这个库(就像我迄今为止使用的大多数xmpp库一样)来看,它是异步执行的,因此我首先调用JabberClients的"Connect方法",然后将登录服务器的方法绑定到JabberClient的OnConnect处理程序.

My struggles with finding a C# xmpp library continues as I have now been force to rule out using both the Soapbox Studio SDK and aggXmpp / Matrix SDK. I have moved on to Jabber-net one of the few remaining C# libraries available and am thus far unable to connect to my Openfire Server. From what I understand this library like most xmpp libraries I have used thus far executes asynchronously so I am first calling the JabberClients 'Connect method' and bind a method that Logs into the server to the JabberClient's OnConnect handler.

下面是我的Connect方法,该方法连接到xmpp服务器,并将jabberClient的OnConnect处理程序绑定到Logon方法.

Below is my Connect method that connects to the xmpp sever and binds the jabberClient's OnConnect handler to the Logon method.

public string Connect()
{
        try
        {
            jabberClient = new JabberClient();
            jabberClient.Connect();
            jabberClient.OnConnect += (o, e) => Logon();                
            jabberClient.OnAuthError += (o, e) => ThrowError("authError");
            jabberClient.OnStreamError += (o, e) => ThrowError("streamError");

            return "Connection Succesful";
        }
        catch (Exception ex)
        {
            _logger.LogError("SessionManager", "Connect", "Could not connect to Openfire Server", ex.ToString());
            return "Could not Connect to Openfire Server: " + ex;
        }
}

产生以下发现的SRV输出:denjab2.jabber.com:5222, 但这会在到达jabberClinet的Connect方法时引发类型为'System.FormatException'的第一个更改异常.然后输出"ExecuteConnect"

Which produces the following output SRV found: denjab2.jabber.com:5222, but this throws a first change exception of type 'System.FormatException' when it reaches the jabberClinet's Connect method. It then outputs 'ExecuteConnect'

下面是登录方法

private void Logon()
{
        JID jid = new JID(OPENFIRE_USER_NAME, OPENFIRE_SERVER, "protoTest");
        jabberClient.User = jid.User;
        jabberClient.Server = jid.Server;
        jabberClient.Password = OPENFIRE_PASSWORD;
        jabberClient.AutoLogin = true;
        jabberClient.AutoPresence = true;
        jabberClient.Login();  
}

此方法不会引发任何异常.

This method throws no exceptions.

此外,将调用JabberClient的onStreamError处理函数,并简单地产生一个异常,该异常读取为发生了流错误",并且不提供内部异常.

Additionally, The JabberClient's onStreamError handler get called and simply procudes an exception that reads "A Stream Error Occured" and provides no inner exception.

我试图通过网络和产品文档研究这些错误,但是似乎没有建立与服务器连接的指南.我必须假设我要么错过了一个步骤,没有提供所有必要的凭据,要么只是执行了以错误顺序登录服务器所需的步骤.

I have tried to research these errors both on the web and through the products documentation but there seem to be no guides to establishing a connection to the server. I have to assume that I am either missing a step, not providing all of the necessary credentials, or am simply executed the steps needed to log on to the server in the wrong order.

研究了Joe Hildebrand的答案中提到的示例应用程序后,我将"Connect"方法修改为如下所示.

After looking into the sample application mentioned in Joe Hildebrand's answer, I modified my 'Connect' method to look like the following.

public void Connect()
{
        try
        {
            jabberClient = new JabberClient();              

            //Bind the JabberClient events to methods that handle those events.
            jabberClient.OnAuthError += (o, e) => ThrowError(ErrorType.AuthError);
            jabberClient.OnStreamError += (o, e) => ThrowError(ErrorType.StreamError);
            jabberClient.OnError += (o, e) => ThrowError(ErrorType.UnknownError);
            jabberClient.OnConnect += (o, e) => ConnectionComplete();

            //Set client settings
            jabberClient.AutoReconnect = 3f;

            JID jid = new JID(OPENFIRE_USER_NAME, OPENFIRE_SERVER, "gec2o");
            jabberClient.User = jid.User;
            jabberClient.Server = jid.Server;
            jabberClient.NetworkHost = null;
            jabberClient.Port = OPENFIRE_NOT_ENCRYPTED_PORT;
            jabberClient.Resource = jid.Resource;
            jabberClient.Password = OPENFIRE_PASSWORD;
            jabberClient.AutoStartTLS = false;
            jabberClient.AutoPresence = false;

            CapsManager cm = new CapsManager();
            cm.Stream = jabberClient;


            jabberClient.Connect();

        }
        catch (Exception ex)
        {
            _log.LogError("ConnectionManager", "Connect", "Could not connect to Openfire Server", ex.ToString());
            throw ex;
    }
}

但是,这似乎会导致相同的行为,并在控制台中显示其他错误,指出"Sock errno:10061",并引发异常,指出无法建立连接,因为目标机器主动拒绝了它"这表明由于某种原因,openfire服务器被拒绝了我的连接请求.我在Jabber-net Google网上论坛上找到了此主题提示我使用服务器的IP地址而不是服务器名称,因为该错误可能与无法解决IPv4 vs IPv6问题有关.

However, this seems to result in the same behavior with the additional errors showing up in the console stating 'Sock errno: 10061' and an exception being thrown stating that 'No connection could be made because the target machine actively refused it' which indicates that for some reason the openfire server is rejected my connection request. I found This topic on the Jabber-net Google Group that suggest I use the server's IP address instead of server name due to the error possibly having something to do with the inability to resolve an IPv4 vs IPv6 issue.

使用已寻址的完整IP摆脱了目标机被主动拒绝"错误,但出现了以下错误

Using the full IP addressed got rid of the 'Target Machine Actively Refused' error but presented me with following error

 SEND: <iq id="JN_1" type="set" to="192.168.1.182"><bind 
xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>rsup</resource></ 
bind></iq> 
RECV: <iq type="error" id="JN_1" from="192.168.1.182" to="infotel/ 
bf18b431"><bind xmlns="urn:ietf:params:xml:ns:xmpp- 
bind"><resource>rsup</resource></bind><error code="400" 
type="modify"><bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/ 
></error></iq> 

ERROR: jabber.connection.sasl.AuthenticationFailedException: Error 
binding resource: <error code="400" type="modify" 
xmlns="jabber:client"><bad-request xmlns="urn:ietf:params:xml:ns:xmpp- 
stanzas" /></error> 

提到此错误,但在此Jabber线程中并未完全解决-net Google网上论坛

通过将AutoStartTLS设置为true并为OnInvalidCertifcate创建事件处理程序并将其绑定到jabber客户端,我能够消除基于Joe Hildebrand注释的错误.

I was able to get rid of this error based on Joe Hildebrand comments by settings AutoStartTLS to true and creating an event handler for OnInvalidCertifcate and binding it to the jabber client.

这是修改后的连接"方法

Here is the revised 'Connect' method

public void Connect()
{
        try
        {
            jabberClient = new JabberClient();

            //Bind the JabberClient events to methods that handle those events.
            jabberClient.OnAuthError += (o, e) => ThrowError(ErrorType.AuthError);
            jabberClient.OnStreamError += (o, e) => ThrowError(ErrorType.StreamError);
            jabberClient.OnError += (o, e) => ThrowError(ErrorType.UnknownError);
            jabberClient.OnConnect += (o, e) => ConnectionComplete();
            jabberClient.OnInvalidCertificate += new System.Net.Security.RemoteCertificateValidationCallback(OnInvalidCertificate);

            //Set client settings
            jabberClient.AutoReconnect = 3f;

            JID jid = new JID(OPENFIRE_USER_NAME, OPENFIRE_SERVER, "gec2o");
            jabberClient.User = jid.User;
            //jabberClient.Server = jid.Server;
            jabberClient.Server = "192.168.97.26";

            jabberClient.NetworkHost = null;
            jabberClient.Port = OPENFIRE_NOT_ENCRYPTED_PORT;
            jabberClient.Resource = jid.Resource;
            jabberClient.Password = OPENFIRE_PASSWORD;
            jabberClient.AutoStartTLS = true;
            jabberClient.AutoPresence = false;

            CapsManager cm = new CapsManager();
            cm.Stream = jabberClient;


            jabberClient.Connect();

}

这是OnInvalidCertifcate事件处理程序

And here is the OnInvalidCertifcate event handler

bool OnInvalidCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
        Console.WriteLine("Invalid certificate ({0}):\n{1}", sslPolicyErrors.ToString(), certificate.ToString(true));
        return true;
}

这导致以下异常.

Error binding resource 
<error type="modify" code="400"><bad-requestmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /></error>

我还应该注意,jabber-net示例控制台应用程序也不适合我.

I should also note that the jabber-net sample Console Application does not work for me either.

这是我尝试运行该应用程序时的命令行输入.

This is my command line input when trying to run the app.

ConsoleClient.exe /j 800802@palburtus/gec2o /r 800802 /p 800802 /o 5222 /t true /n null

这是我收到的输出

  Connecting
Connected
ERROR: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it at System.Net.Sockets.Socket.EndConnect(IAsyncResult asy
   at bedrock.net.AsyncSocket.ExecuteConnect(IAsyncResult a
.0.710\bedrock\net\AsyncSocket.cs:line 782

希望有人可以帮助解释我做错了什么.

Hopefully someone can help explain what I'm doing wrong.

推荐答案

您是否尝试过 ConsoleClient 应用程序?

Have you tried the Example or ConsoleClient application?

在完全配置JabberClient实例之前,您要调用Connect().首先设置所有属性和回调,然后调用Connect().最后,除非AutoLoginFalseOnAuthError已触发,否则您几乎永远不要调用Login().

You're calling Connect() before your have your JabberClient instance fully configured. Set up all of your properties and callbacks first, then call Connect(). Finally, you should almost never call Login() unless AutoLogin is False or OnAuthError has fired.

这篇关于使用Jabber-Net连接到Openfire服务器时错误绑定资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆