Question is regarding Invalid SSL Certificate
. I have been trying to connect to Websocket
using SocketIOClient
. But getting Invalid SSL certificate issue. The code used to connect Socket is
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")
}
}
}