I'm using NWBrowser to search for a server that I hosted. The browser does find my service but when it tries to connect to it, it gets stuck in the preparing phase in NWConnection.stateUpdateHandler. When I hardcode the local IP address of my computer (where the server is hosted) into NWConnection it works perfectly fine and is able to connect.
When it gets stuck in the preparing phase, it gives me the warnings and error messages in the image below. You can also see that the service name is correct and it is found.
I have tried _http._tcp
and _ssh._tcp
types and neither work.
This is what my code looks like:
func findServerAndConnect(port: UInt16) {
print("Searching for server...")
let browser = NWBrowser(for: .bonjour(type: "_ssh._tcp", domain: "local."), using: .tcp)
browser.browseResultsChangedHandler = { results, _ in
print("Found results: \(results)")
for result in results {
if case let NWEndpoint.service(name, type_, domain, interface) = result.endpoint {
if name == "PocketPadServer" {
print("Found service: \(name) of type \(type_) in domain \(domain) on interface \(interface)")
// Construct the full service name, including type and domain
let fullServiceName = "\(name).\(type_).\(domain)"
print("Full service name: \(fullServiceName), \(result.endpoint)")
self.connect(to: result.endpoint, port: port)
browser.cancel()
break
}
}
}
}
browser.start(queue: .main)
}
func connect(to endpoint: NWEndpoint, port: UInt16) {
print("Connecting to \(endpoint) on port \(port)...")
// endpoint = NWEndpoint(
let tcpParams = NWProtocolTCP.Options()
tcpParams.enableFastOpen = true
tcpParams.keepaliveIdle = 2
let params = NWParameters(tls: nil, tcp: tcpParams)
params.includePeerToPeer = true
// connection = NWConnection(host: NWEndpoint.Host("xx.xxx.xxx.xxx"), port: NWEndpoint.Port(3000), using: params)
connection = NWConnection(to: endpoint, using: params)
connection?.pathUpdateHandler = { path in
print("Connection path update: \(path)")
if path.status == .satisfied {
print("Connection path is satisfied")
} else {
print("Connection path is not satisfied: \(path.status)")
}
}
connection?.stateUpdateHandler = { newState in
DispatchQueue.main.async {
switch newState {
case .ready:
print("Connected to server")
self.pairing = true
self.receiveMessage()
case .failed(let error):
print("Connection failed: \(error)")
self.isConnected = false
case .waiting(let error):
print("Waiting for connection... \(error)")
self.isConnected = false
case .cancelled:
print("Connection cancelled")
self.isConnected = false
case .preparing:
print("Preparing connection...")
self.isConnected = false
default:
print("Connection state changed: \(newState)")
break
}
}
}
connection?.start(queue: .main)
}