Thanks a lot! You sold all my problems))) Now I'll be able to complete my app and send it to appstore. I hope this thread will be useful for others, because there are quite a few examples about bonjour, especially with comments )
Post
Replies
Boosts
Views
Activity
Thank you so much for helping! But I have same problem with your code - its working as expected, but in the end, when I commented out
language
exit(EXIT_SUCCESS)
my app just freezing partially - I can go back to previous view and even change any sliders, but all buttons and navigation stop working. I have three views in app - main, additional one and settings. After starting I go to "settings" view and push button "find lamp", that call "main" resolving function from you example. Then I receive "host resolved" and may go back to main view, but then it partially stops working, as I described above...
Update: changing this fixed problem:
language
RunLoop.current.run(until: Date(timeIntervalSinceNow: 5))
Thank you for this fully commented example, it's very helpful! It works for resolving known name, but every lamp has its individual name, depending on esp8266's id (EmbUI-XXXXXX) and my goal not only to resolve it, but to find in local network as well.
And the part for finding host name would be nearly this size too? I surprised that there is so much coding for such standard situation)
I've left only common code for my purpose:
language
class BrowserAgent : NSObject, NetServiceBrowserDelegate {
var currentService:NetService?
func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
Lamp.lampHost = service.name+".local"
}
}
....
let agent = BrowserAgent()
let browser = NetServiceBrowser()
browser.stop()
browser.delegate = agent
browser.schedule(in: RunLoop.main, forMode: .default)
browser.searchForServices(ofType: "_http._tcp", inDomain: "local.")
RunLoop.main.run()
But it still freezing my app just after service found
Thank you for such detailed answer!) I came to thought that maybe it'll be better to find host and then communicate by host, not IP. Just adding ".local" to the host name solved the problem. So I've already managed how to find host name. Something like this:
swift
class ServiceAgent : NSObject, NetServiceDelegate {
func netServiceDidResolveAddress(_ sender: NetService) {
if let data = sender.txtRecordData() {
let dict = NetService.dictionary(fromTXTRecord: data)
}
}
}
class BrowserAgent : NSObject, NetServiceBrowserDelegate {
var currentService:NetService?
let serviceAgent = ServiceAgent()
func netServiceBrowser(_ browser: NetServiceBrowser, didFindDomain domainString: String, moreComing: Bool) {
print("domain found: \(domainString)")
}
func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
Lamp.lampHost = service.name+".local"
self.currentService = service
service.delegate = self.serviceAgent
service.resolve(withTimeout: 5)
}
}
let agent = BrowserAgent()
let browser = NetServiceBrowser()
browser.stop()
browser.delegate = agent
browser.schedule(in: RunLoop.current, forMode: .default)
browser.searchForServices(ofType: "_http._tcp", inDomain: "local.")
RunLoop.main.run()
But so far this code after finding my host name just freezing my app forever.
I'm planning to send http requests. I've already done with this part - all is working as expected, but I have to enter IP manually. All I want to do - search local network for "_http._tcp" services with name starting with "embui...", find their IPs and save for later use.