我正在尝试使用新的Network.framework连接到WebSocket,但是面对来自服务器的零握手响应。
(是的,我知道红蜘蛛是存在的,但它不支持网络接口之间用户交换的代理/移动性)
我的测试代码:
func beginTest() {
let connection = NWConnection(host: "echo.websocket.org", port: 443, using: .tls)
connection.stateUpdateHandler = { state in
print("State:", state)
switch state {
case .ready:
self.connectionReady(connection)
default:
break
}
}
connection.start(queue: .main)
}
func connectionReady(_ connection: NWConnection) {
let raw = """
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: echo.websocket.org
Origin: https://echo.websocket.org
Sec-WebSocket-Key: s04nPqA7M6pQ3Lu2jRJLSQ==
Sec-WebSocket-Version: 13
"""
let rawData = raw.appending("\n\n\n").replacingOccurrences(of: "\n", with: "\r\n").data(using: .utf8)
connection.send(content: rawData!, completion: .idempotent)
connection.receiveMessage(completion: {data, context, bool, error in
if let data = data {
print("Received:", String(data: data, encoding: .utf8))
}
print("Error:", error)
let hello = "Hello".data(using: .utf8)
connection.send(content: hello, completion: .idempotent)
})
}它的零响应和连接被删除,而不是从服务器获得升级握手响应,下面是控制台日志:
State: preparing
State: ready
Received: nil
Error: nil
2018-10-08 11:38:57.314885+0800 SwiftNetworkTest[86448:3026660] [] nw_socket_handle_socket_event [C1.1:2] Socket SO_ERROR [54: Connection reset by peer]有人能指导我如何使用苹果新Network.framework吗?我们会非常感激的!
Update1
.ascii编码而不是.utf8的握手响应。Connection reset by peer。如何在升级到WebSocket后保留连接?发布于 2018-11-05 13:28:09
发布于 2020-07-30 22:17:10
在阅读了文档之后:result
我像这样设置了经过身份验证的TLS连接:
init(endpoint: NWEndpoint, interface: NWInterface?, passcode: String, delegate: BitfinexConnectionDelegate)
{
self.delegate = delegate
self.initiatedConnection = false
let host = "api.bitfinex.com"
let port = 443
let options = NWProtocolTCP.Options()
options.connectionTimeout = 15
let tlsOptions = NWProtocolTLS.Options()
sec_protocol_options_set_verify_block(
tlsOptions.securityProtocolOptions,
{
(sec_protocol_metadata, sec_trust, sec_protocol_verify_complete) in
let trust = sec_trust_copy_ref(sec_trust).takeRetainedValue()
let pinner = FoundationSecurity()
pinner.evaluateTrust(trust: trust, domain: host, completion:
{
(state) in
switch state
{
case .success:
sec_protocol_verify_complete(true)
case .failed(_):
sec_protocol_verify_complete(false)
}
})
}, queue
)
let parameters = NWParameters(tls: tlsOptions, tcp: options)
let conn = NWConnection(host: NWEndpoint.Host.name(host, nil),
port: NWEndpoint.Port(rawValue: UInt16(port))!,
using: parameters
)
self.connection = conn
startConnection()
}FoundationSecurity()块是一个简单的TLS计算
if SecTrustEvaluateWithError(trust, &error)
{
completion(.success)
}
else
{
completion(.failed(error))
}一旦我的连接好了。我发送了一个像这样创建的数据对象。这取决于您要与之接口的API。
private func prepareWebSocket() throws -> Data
{
let apiKey = "API_KEY"
let apiSecret = "API_SECRET"
let authNonce = NonceProvider.sharedInstanse.nonce
let authPayload = "AUTH\(authNonce)"
let authenticationKey = SymmetricKey(data: apiSecret.data(using: .ascii)!)
let authenticationCode = HMAC<SHA384>.authenticationCode(for: authPayload.data(using: .ascii)!,
using: authenticationKey
)
let authSig = authenticationCode.compactMap { String(format: "%02hhx", $0) }.joined()
let payload: [String : Any] =
[
"event": "auth",
"apiKey" : apiKey,
"authSig": authSig,
"authPayload": authPayload,
"authNonce": authNonce
]
return try JSONSerialization.data(withJSONObject: payload, options: .fragmentsAllowed)
}https://stackoverflow.com/questions/52695147
复制相似问题