gpt4 book ai didi

swift - SocketIOClient : Handling event: error with data: ["Invalid SSL certificate"]

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:56 26 4
gpt4 key购买 nike

问题是关于无效的 SSL 证书。我一直在尝试使用 SocketIOClient 连接到 Websocket。但是遇到无效的 SSL 证书问题。用于连接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 证书(.cer 文件)。就是这样!!

解决方案二:

*如果您不需要自签名和 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),
]

关于swift - SocketIOClient : Handling event: error with data: ["Invalid SSL certificate"],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50960784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com