问题
Error: Optional(["message": Error during negotiation request.])
I'm getting the error while connection signalr server, I think there is an issue with server side as they used self signed certificate. How I can fix by client side(swift),how to enable in iOS 11 to work with self signed certificate? For the signer library.
Below is the my code:
func test() {
let persistentConnection = SignalR("http://services.test.com/signalr", connectionType: .persistent)
let simpleHub1 = Hub("testHub")
persistentConnection.useWKWebView = false
persistentConnection.addHub(simpleHub1)
persistentConnection.received = { data in
print(data)
}
persistentConnection.connected = { [weak self] in
print("Connected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")
}
persistentConnection.starting = { [weak self] in
print("Starting...")
}
persistentConnection.reconnecting = { [weak self] in
print("Reconnecting...")
}
persistentConnection.connected = { [weak self] in
print("Connected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")
}
persistentConnection.reconnected = { [weak self] in
print("Reconnected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")
}
persistentConnection.disconnected = { [weak self] in
print("Disconnected.")
}
persistentConnection.connectionSlow = { print("Connection slow...") }
persistentConnection.error = { [weak self] error in
print("Connection timed out. Restarting...")
persistentConnection.start()
}
}
persistentConnection.start()
}
回答1:
iOS does not allow self signed certificates for good reasons. You should always try to get a valid certificate for your server. It is quite easy to obtain a valid server certificate from Let's Encrypt for free.
If you really need to use own certificates, sign them with an own certificate authority (CA) and export the CA certificate to your device. Your device will then accept all certificates signed by this CA.
You can use the following OpenSSL calls to create a certificate authority and sign your own certificates with it:
openssl genrsa -out ca.key.pem 2048
openssl req -x509 -new -days 365 -sha256 -nodes -subj '/C=Country/ST=State/L=Location/CN=CommonName' -key ca.key.pem -out ca.crt.pem
openssl genrsa -out server.key.pem 2048
openssl req -new -sha256 -subj '/C=Country/ST=State/L=Location/CN=CommonName' -key
server.key.pem -out server.csr.pem
openssl x509 -req -days 365 -in server.csr.pem -CA ca.crt.pem -CAkey ca.key.pem -CAcreateserial -out server.crt.pem -sha256
openssl x509 -in server.crt.pem -text -noout
openssl verify -CAfile ca.crt.pem server.crt.pem
来源:https://stackoverflow.com/questions/48484137/self-signed-certificate-error-with-signalr-error-during-negotiation-request-io