Currently I'm using NetServiceBrowser
to find Bonjour services and to resolve corresponding addresses and port.
Looking to de-complicate my code I stumbled upon NWBrowser
which seems to provide a very simple interface to deal with the Bonjour discovering.
However, the browseResultsChangedHandler
sends back results and changes which contain an endpoint of enum case service
. I'm trying to get address and port information from the results, but is seems the NWEndpoint
would have to be of enum type .hostPort.
Ideally I would use the endpoint to connect to servers using NWConnection
, however, I'm using another library which doesn't handle the NWEndpoint
directly.
Are there (simple) ways of getting addresses and port information from an NWEndpoint.service
result?
import Foundation
import Network
let browser = NWBrowser(for: .bonjour(type: "_http._tcp", domain: ""), using: NWParameters())
browser.browseResultsChangedHandler = { (results, changes) in
print("Results:")
for result in results
{
if case .service(let service) = result.endpoint
{
debugPrint(service)
}
else
{
assert(false, "This nevers gets executed")
}
}
print("Changes:")
for change in changes
{
if case .added(let added) = change
{
if case .service(let service) = added.endpoint
{
debugPrint(service)
}
else
{
assert(false, "This nevers gets executed")
}
}
}
}
browser.start(queue: DispatchQueue.main)
sleep(3)