【问题标题】:XMPPFramework SSL connection with open fire server is not connecting与开火服务器的 XMPPFramework SSL 连接未连接
【发布时间】:2014-09-09 15:09:41
【问题描述】:

在我的 delegate.m 中

 - (void)setupStream
{
NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");
customCertEvaluation = YES;

//  allowSelfSignedCertificates = YES;
//  allowSSLHostNameMismatch = NO;  // Setup xmpp stream
// 
// The XMPPStream is the base class for all activity.
// Everything else plugs into the xmppStream, such as modules/extensions and delegates.

xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

#if !TARGET_IPHONE_SIMULATOR
{
    // Want xmpp to run in the background?
    // 
    // P.S. - The simulator doesn't support backgrounding yet.
    //        When you try to set the associated property on the simulator, it simply fails.
    //        And when you background an app on the simulator,
    //        it just queues network traffic til the app is foregrounded again.
    //        We are patiently waiting for a fix from Apple.
    //        If you do enableBackgroundingOnSocket on the simulator,
    //        you will simply see an error message from the xmpp stack when it fails to set the property.

    xmppStream.enableBackgroundingOnSocket = YES;
}
#endif

// Setup reconnect
// 
// The XMPPReconnect module monitors for "accidental disconnections" and
// automatically reconnects the stream for you.
// There's a bunch more information in the XMPPReconnect header file.

xmppReconnect = [[XMPPReconnect alloc] init];

//  XMPPAutoPing *xmppAutoPing =  [[XMPPAutoPing alloc]    initWithDispatchQueue:dispatch_get_main_queue()];
//xmppAutoPing.pingInterval = 25.f; // default is 60
//xmppAutoPing.pingTimeout = 10.f; // default is 10
//[xmppAutoPing addDelegate:self delegateQueue:dispatch_get_main_queue()];
//[xmppAutoPing activate:self.xmppStream];

// Setup roster
// 
// The XMPPRoster handles the xmpp protocol stuff related to the roster.
// The storage for the roster is abstracted.
// So you can use any storage mechanism you want.
// You can store it all in memory, or use core data and store it on disk, or use core data with an in-memory store,
// or setup your own using raw SQLite, or create your own storage mechanism.
// You can do it however you like! It's your application.
// But you do need to provide the roster with some storage facility.

xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];

xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];

xmppRoster.autoFetchRoster = YES;
xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;

// Setup vCard support
// 
// The vCard Avatar module works in conjuction with the standard vCard Temp module to download user avatars.
// The XMPPRoster will automatically integrate with XMPPvCardAvatarModule to cache roster photos in the roster.

xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];

xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];

// Setup capabilities
// 
// The XMPPCapabilities module handles all the complex hashing of the caps protocol (XEP-0115).
// Basically, when other clients broadcast their presence on the network
// they include information about what capabilities their client supports (audio, video, file transfer, etc).
// But as you can imagine, this list starts to get pretty big.
// This is where the hashing stuff comes into play.
// Most people running the same version of the same client are going to have the same list of capabilities.
// So the protocol defines a standardized way to hash the list of capabilities.
// Clients then broadcast the tiny hash instead of the big list.
// The XMPPCapabilities protocol automatically handles figuring out what these hashes mean,
// and also persistently storing the hashes so lookups aren't needed in the future.
// 
// Similarly to the roster, the storage of the module is abstracted.
// You are strongly encouraged to persist caps information across sessions.
// 
// The XMPPCapabilitiesCoreDataStorage is an ideal solution.
// It can also be shared amongst multiple streams to further reduce hash lookups.

xmppCapabilitiesStorage = [XMPPCapabilitiesCoreDataStorage sharedInstance];
xmppCapabilities = [[XMPPCapabilities alloc] initWithCapabilitiesStorage:xmppCapabilitiesStorage];

xmppCapabilities.autoFetchHashedCapabilities = YES;
xmppCapabilities.autoFetchNonHashedCapabilities = NO;

// Activate xmpp modules

[xmppReconnect         activate:xmppStream];
[xmppRoster            activate:xmppStream];
[xmppvCardTempModule   activate:xmppStream];
[xmppvCardAvatarModule activate:xmppStream];
[xmppCapabilities      activate:xmppStream];

// Add ourself as a delegate to anything we may be interested in

[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];

// Optional:
// 
// Replace me with the proper domain and port.
// The example below is setup for a typical google talk account.
// 
// If you don't supply a hostName, then it will be automatically resolved using the JID (below).
// For example, if you supply a JID like 'user@quack.com/rsrc'
// then the xmpp framework will follow the xmpp specification, and do a SRV lookup for quack.com.
// 
// If you don't specify a hostPort, then the default (5222) will be used.

[xmppStream setHostName:@"10.10.1.77"];
[xmppStream setHostPort:5222];



// You may need to alter these settings depending on the server you're connecting to
//  allowSelfSignedCertificates = YES;
// allowSSLHostNameMismatch = NO;

customCertEvaluation = YES;
}

还有

- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
{
 DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

NSString *expectedCertName = [xmppStream.myJID domain];
if (expectedCertName)
{
    [settings setObject:expectedCertName forKey:(NSString *)kCFStreamSSLPeerName];
}

if (customCertEvaluation)
        [settings setObject:@(YES) forKey:GCDAsyncSocketManuallyEvaluateTrust];
}



- (void)xmppStream:(XMPPStream *)sender didReceiveTrust:(SecTrustRef)trust
                                  completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler
{
 /*DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

  // The delegate method should likely have code similar to this,
  // but will presumably perform some extra security code stuff.
   // For example, allowing a specific self-signed certificate that is known to the app.
     allowSelfSignedCertificates = YES;
    allowSSLHostNameMismatch = NO;
     dispatch_queue_t bgQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(bgQueue, ^{

    SecTrustResultType result = kSecTrustResultDeny;
    OSStatus status = SecTrustEvaluate(trust, &result);

    if (status == noErr && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
        completionHandler(YES);
    }
    else {
        completionHandler(NO);
    }

});
*/
completionHandler(YES);

}

我已经完成了代码中建议的所有操作,但仍然使用 SSL 端口连接到服务器出现错误

2014-07-18 18:08:14:724 iPhoneXMPP[20593:60b] iPhoneXMPPAppDelegate: xmppStream:socketDidConnect:
2014-07-18 18:08:14:724 iPhoneXMPP[20593:60b] iPhoneXMPPAppDelegate: xmppStream:socketDidConnect:
2014-07-18 18:08:14:925 iPhoneXMPP[20593:60b] iPhoneXMPPAppDelegate: xmppStreamDidDisconnect:withError:
2014-07-18 18:08:14.925 iPhoneXMPP[20593:60b] Unable to connect to server
2014-07-18 18:08:14:926 iPhoneXMPP[20593:60b] Unable to connect to server. Check xmppStream.hostName

我应该如何解决这个错误;连接到普通端口没问题。连接到 SSL 端口是唯一的问题。

【问题讨论】:

  • 在xmppStreamDidDisconnect:withError: 中记录错误,看看它说了什么......
  • @vishnuvarthan 你有没有实现过文件传输功能?如果是,那么请您帮忙解决一些有关文件传输的问题。
  • @ashishchaklasiya 我会尝试战争,但我有关于将证书发送到服务器的问题任何帮助??
  • @vishnuvarthan 您要向服务器发送哪个证书?
  • 服务器提供的证书。它捆绑在我的应用程序中(.p12)

标签: ios objective-c ssl xmppframework


【解决方案1】:

我终于可以在 5223 端口使用 SSL。我必须在XMPPStream.m 上强制使用startTLS 上的didConnectToHost。我不知道为什么isSecure 总是说NO。

【讨论】:

    【解决方案2】:

    使用以下方法启用 SSL/TLS。但是这个方法在 XMPPStream.h 类中没有定义,你需要在这里定义它并从 appdelegate 或 setupsteam 方法内部访问它。

    -(void)setIsSecure:(BOOL)flag
    

    【讨论】:

      猜你喜欢
      • 2015-05-03
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多