How to handle a connection timing out?
// `endpoint` was obtained from a bonjour browser
let connection = NWConnection(to: endpoint, using: .tcp)
connection.stateUpdateHandler = { [self] newState in
print("newState: \(newState)")
}
connection.start(queue: .main)
Log:
newState: preparing
nw_socket_handle_socket_event [C1.1.4.1:1] Socket SO_ERROR [60: Operation timed out]
... several more times
The stateUpdateHandler function is never called again.
How to handle a connection timing out?
NWConnection
does not, in general, implement timeouts. If you want that, you have to implement it yourself using a timer.
I’m curious about your setup though. When things fail in this way, is it because the server is not responding? Or is the server up’n’running but the connection is failing for some reason?
Also, what platform? And what version of that platform?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"