我曾多次尝试通过 Starscream 与 SSL 建立连接,但由于某些原因,我遇到了同样的错误。以下是简单的连接和响应
client = WebSocket(url: URL(string: "wss://XXXXXXXX:443/")!)
client?.enabledSSLCipherSuites = [TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256]
let certificateData = try Data(contentsOf:Bundle.main.url(forResource: "certificate", withExtension: "cer")!)
let cert = SSLCert(data: certificateData)
let security = SSLSecurity(certs: [cert], usePublicKeys: true)
security.validatedDN = false
security.validateEntireChain = false
client?.security = security
client?.delegate = self
client?.connect()
第二个
do
{
client = WebSocket(url: URL(string: "wss://XXXXXXXX:443/")!)
let urlPath = Bundle.main.path(forResource: "certificate", ofType: "der")
let url = NSURL.fileURL(withPath: urlPath!)
let certificateData = try Data(contentsOf: url)
let certificate: SecCertificate =
SecCertificateCreateWithData(kCFAllocatorDefault, certificateData as CFData)!
var trust: SecTrust?
let policy = SecPolicyCreateBasicX509()
let status = SecTrustCreateWithCertificates(certificate, policy, &trust)
if status == errSecSuccess {
let key = SecTrustCopyPublicKey(trust!)!;
let ssl = SSLCert(key: key)
let security = SSLSecurity(certs: [ssl], usePublicKeys: false)
security.validateEntireChain = false
client?.security = security
client?.delegate = self
client?.connect()
}
}catch let error as NSError
{
print(error)
}
和日志
CFNetwork SSLHandshake 失败 (-9807)
0 TCP 连接 0x600003119c80 SSLHandshake 失败 (-9807)
ERROR - %@ Optional("操作无法完成。(OSStatus error -9807.)")