我正在使用 NSNetServiceBrowser 并且能够在“didFindService”中找到由 AVAHI 发布的服务列表,如下所示:服务出现:本地。_https._tcp。TEMP-Mobileyes5-1C497B9ED382 -1 服务出现:本地。_https._tcp。TEMP-Mobileyes5-1C497B8E3916 -1 服务出现:本地。_https._tcp。TEMP-Mobileyes5-1C497B9ED380 -1
但无法找到同一服务的 IP 地址和端口号。我发现“netServiceDidResolveAddress”中的代码没有到达我的代码是:
class ServiceDiscovery : NSObject, NSNetServiceBrowserDelegate,NSNetServiceDelegate {
var _browser:NSNetServiceBrowser!
var _service: NSNetService!
var services = [NSNetService]()
override init() {
_browser = NSNetServiceBrowser()
super.init()
_browser.delegate = self
_browser.includesPeerToPeer = true
_browser.searchForServicesOfType("_https._tcp.", inDomain: "local.")
_browser.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
func netServiceBrowser(browser: NSNetServiceBrowser, didFindDomain domainString: String, moreComing: Bool) {
print(domainString)
}
func netServiceBrowser(aNetServiceBrowser: NSNetServiceBrowser, didFindService aNetService: NSNetService, moreComing: Bool) {
print("Service appeared: \(aNetService)")
services.append(aNetService)
aNetService.delegate = self
aNetService.resolveWithTimeout(5.0)
}
func netServiceBrowser(browser: NSNetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
print(errorDict)
}
func netServiceBrowser(browser: NSNetServiceBrowser, didRemoveService service: NSNetService, moreComing: Bool) {
print("Service removed: \(service)")
}
func netService(sender: NSNetService, didNotResolve errorDict: [String : NSNumber]) {
print(errorDict)
}
func netServiceDidResolveAddress(sender: NSNetService) {
print(sender.addresses![0])
}
}