【发布时间】:2018-06-21 05:11:55
【问题描述】:
问题是关于无效的SSL Certificate。我一直在尝试使用SocketIOClient 连接到Websocket。但是遇到 Invalid SSL certificate 问题。用于连接Socket的代码是
import UIKit
import SocketIO
class SocketIOManager: NSObject, URLSessionDelegate {
static let shared = SocketIOManager()
var socket: SocketIOClient!
func socketConnect() {
let token = "ggggggg" //some token
let url = URL(string: "https://sample.com") // some https url
let specs: SocketIOClientConfiguration = [
.connectParams(["access_token": token]),
.log(true),
.forceNew(true),
.selfSigned(true),
.forcePolling(true),
.secure(true),
.reconnects(true),
.forceWebsockets(true),
.reconnectAttempts(3),
.reconnectWait(3),
.security(SSLSecurity(usePublicKeys: true)),
.sessionDelegate(self),
]
socket = SocketIOClient(socketURL: url!, config: specs)
socket.on(clientEvent: .connect) {data, ack in
print("socket connected")
self.socket.emitWithAck("emit", with: []).timingOut(after: 2, callback: { (data) in
print(data)
})
}
socket.on("fetch") { (dataArray, ack) in
print(dataArray)
}
socket.on(clientEvent: .error) {data, ack in
print(data)
}
socket.on(clientEvent: .disconnect) {data, ack in
print(data)
}
socket.onAny { (data) in
// print(data)
}
socket.connect()
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
// We've got an error
if let err = error {
print("Error: \(err.localizedDescription)")
} else {
print("Error. Giving up")
}
}
}
【问题讨论】:
-
您是否在主包中添加了 ssl 证书?
-
没有。我没有 SSL 证书。但是其他 Socket IO 客户端在没有 SSL 证书的情况下访问,例如具有默认 SSL 配置的 Android 和 Javascript。
-
Patel,我们需要从服务器端生成的证书还是我们可以使用从钥匙串生成的证书。
-
您好,Harshal,感谢您的回答。但是您使用了 HTTP,当使用 HTTPS 时,我们遇到了“无效的 SSL 证书”问题。
标签: swift ssl websocket socket.io ssl-certificate