WSS/TLS websocket connection with Swift iOS



已解决(以下答案(

正在使用Starscream库在测试服务器中创建安全的websocket wss,我们有一个自签名证书,我发现无法建立连接。

var socket = WebSocket(url: NSURL(scheme: "wss", host: "selfsignedserver.com", path: "/")!)

日志

2014-12-16 10:38:10.260 pruebasignin[2135:363455] CFNetwork SSLHandshake failed (-9807)
websocket is disconnected: The operation couldn’t be completed. (OSStatus error -9807.)

当我尝试连接到有效的服务器证书时,也无法连接解决

var socket = WebSocket(url: NSURL(scheme: "wss", host: "production.com", path: "/")!)

日志

websocket is disconnected: Invalid HTTP upgrade

Starscream现在支持一个标志,因此您可以使用自签名证书:https://github.com/daltoniam/Starscream/blob/bf0146db269249d200bb3bc4185cb5724cfa2ae8/README.md#self-signed-ssl-and-voip

(为后人编辑;链接到截至2016年4月发布的自述文件(

我通过允许自签名证书Starscream修改库来解决这个问题。为此必须添加 arcivo WebSocket.swift以下代码:

if url.scheme == "wss" || url.scheme == "https" {
        inputStream!.setProperty(NSStreamSocketSecurityLevelNegotiatedSSL, forKey: NSStreamSocketSecurityLevelKey)
        outputStream!.setProperty(NSStreamSocketSecurityLevelNegotiatedSSL, forKey: NSStreamSocketSecurityLevelKey)
        /* My code */
        var settings = Dictionary<NSObject, NSObject>()
        settings[kCFStreamSSLValidatesCertificateChain] = NSNumber(bool:false)
        settings[kCFStreamSSLPeerName] = kCFNull
        CFReadStreamSetProperty(self.inputStream, kCFStreamPropertySSLSettings, settings)
        CFWriteStreamSetProperty(self.outputStream, kCFStreamPropertySSLSettings, settings)
        /* End my code*/
    }

相关内容

  • 没有找到相关文章

最新更新