与Swift iOS的WSS / TLS websocket连接

时间:2014-12-16 10:17:31

标签: ios ssl swift websocket wss

已解决(以下回答)

我正在使用 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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

我通过允许自签名证书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*/

    }