0

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")
        }
    }

}
Tanvir Nayem
  • 702
  • 10
  • 25
Nag
  • 21
  • 1
  • 1
  • 3
  • Did you add ssl certificates in your main bundle? – SPatel Jun 21 '18 at 05:13
  • No. I don't have an SSL Certificate. But other Socket IO clients are accessing without SSL Certificate, like Android and Javascript with default SSL Configuration. – Nag Jun 21 '18 at 05:17
  • its may help you https://stackoverflow.com/a/50062606/5461400 – Harshal Valanda Jun 21 '18 at 05:31
  • Patel, do we need certificate generated from server side or can we use certificate generated from keychain. – Nag Jun 21 '18 at 05:48
  • Hi Harshal, Thanks for your answer. But you used HTTP, When using HTTPS we are getting "Invalid SSL Certificate" issue. – Nag Jun 21 '18 at 06:26

1 Answers1

0

There is two Possible solution

Solution 1:

Just add required ssl certificate(.cer files) in your main bundle. That's it!!

Solution 2:

*If you don't need self signing and SSL Pining then modified your code specs like below code.

let specs: SocketIOClientConfiguration = [
        .connectParams(["access_token": token]),
        .log(true),
        .forceNew(true),
        .forcePolling(true),
        .reconnects(true),
        .forceWebsockets(true),
        .reconnectAttempts(3),
        .reconnectWait(3),
        .sessionDelegate(self),
        ]
SPatel
  • 4,768
  • 4
  • 32
  • 51