【问题标题】:SocketIOClient: Handling event: error with data: ["Invalid SSL certificate"]SocketIOClient:处理事件:数据错误:[“无效的 SSL 证书”]
【发布时间】: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


【解决方案1】:

有两种可能的解决方案

解决方案 1:

只需在您的主包中添加所需的 ssl 证书(.cer 文件)。就是这样!!

解决方案 2:

*如果您不需要自签名和 SSL Pining,请修改您的代码规范,如下面的代码。

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

【讨论】:

  • 谢谢斯帕特尔。我修改了代码,但出现此错误“无效的 HTTP 升级”
  • 尝试移除 forcePolling
  • 好的。但我的回答是关于Invalid SSL Certificate 问题。谢谢,你可以再问一个答案!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2018-11-28
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
相关资源
最近更新 更多