我正在尝试在我的Nativescript应用程序中在iOS上执行Zeroconf / Bonjour发现。我使用的是以下代码,但问题是WillSearch
或DidStopSearch
或其他回调从未触发:
this.netServiceBrowser = NSNetServiceBrowser.new();
this.netServiceBrowser.delegate = <NSNetServiceBrowserDelegate> {
netServiceBrowserDidFindDomainMoreComing: (browser: NSNetServiceBrowser, domainString: string, moreComing: boolean) => {
console.log(`netServiceBrowserDidFindDomainMoreComing: ${domainString}`);
},
netServiceBrowserWillSearch: (browser:NSNetServiceBrowser) => {
console.log(`netServiceBrowserWillSearch`);
},
netServiceBrowserDidStopSearch: (browser:NSNetServiceBrowser) => {
console.log(`netServiceBrowserDidStopSearch`);
},
netServiceBrowserDidFindServiceMoreComing: (browser:NSNetServiceBrowser, service:NSNetService, moreComing:boolean) => {
console.log(`netServiceBrowserDidFindServiceMoreComing, found service ${service.name}`);
console.log(`moreComing: ${moreComing}`);
}
}
this.netServiceBrowser.searchForRegistrationDomains(); // start searching for domains
this.netServiceBrowser.searchForServicesOfTypeInDomain(this.config.bonjour.serviceType, this.config.bonjour.serviceDomain);
setTimeout(() => { this.netServiceBrowser.stop(); },30000); // stop discovery after 30 secs
我的猜测是我在委托中对'回调'的定义做错了,但我不知道是什么。
这是所有类型的相关部分:
declare class NSNetServiceBrowser extends NSObject {
static alloc(): NSNetServiceBrowser; // inherited from NSObject
static new(): NSNetServiceBrowser; // inherited from NSObject
delegate: NSNetServiceBrowserDelegate;
includesPeerToPeer: boolean;
removeFromRunLoopForMode(aRunLoop: NSRunLoop, mode: string): void;
scheduleInRunLoopForMode(aRunLoop: NSRunLoop, mode: string): void;
searchForBrowsableDomains(): void;
searchForRegistrationDomains(): void;
searchForServicesOfTypeInDomain(type: string, domainString: string): void;
stop(): void;
}
interface NSNetServiceBrowserDelegate extends NSObjectProtocol {
netServiceBrowserDidFindDomainMoreComing?(browser: NSNetServiceBrowser, domainString: string, moreComing: boolean): void;
netServiceBrowserDidFindServiceMoreComing?(browser: NSNetServiceBrowser, service: NSNetService, moreComing: boolean): void;
netServiceBrowserDidNotSearch?(browser: NSNetServiceBrowser, errorDict: NSDictionary<string, number>): void;
netServiceBrowserDidRemoveDomainMoreComing?(browser: NSNetServiceBrowser, domainString: string, moreComing: boolean): void;
netServiceBrowserDidRemoveServiceMoreComing?(browser: NSNetServiceBrowser, service: NSNetService, moreComing: boolean): void;
netServiceBrowserDidStopSearch?(browser: NSNetServiceBrowser): void;
netServiceBrowserWillSearch?(browser: NSNetServiceBrowser): void;
}
declare var NSNetServiceBrowserDelegate: {
prototype: NSNetServiceBrowserDelegate;
};
感谢您的任何指示!!
答案 0 :(得分:1)
如果有人对此感兴趣,我可以在NativeScript社区的人的帮助下解决它(谢谢!):
/* Define NSNetServiceBrowserDelegate implementation class */
class MyNSNetServiceBrowserDelegate extends NSObject implements NSNetServiceBrowserDelegate {
public static ObjCProtocols = [NSNetServiceBrowserDelegate];
static new(): MyNSNetServiceBrowserDelegate {
return <MyNSNetServiceBrowserDelegate>super.new();
}
private _callback: (result: string) => void;
public initWithCallback(callback: (result: string) => void): MyNSNetServiceBrowserDelegate {
this._callback = callback;
return this;
}
public netServiceBrowserDidFindDomainMoreComing(browser: NSNetServiceBrowser, domainString: string, moreComing: boolean) {
console.log(`netServiceBrowserDidFindDomainMoreComing: ${domainString}`);
this._callback(domainString);
}
public netServiceBrowserWillSearch(browser:NSNetServiceBrowser) {
console.log(`netServiceBrowserWillSearch`);
}
public netServiceBrowserDidStopSearch(browser:NSNetServiceBrowser) {
console.log(`netServiceBrowserDidStopSearch`);
}
public netServiceBrowserDidFindServiceMoreComing(browser:NSNetServiceBrowser, service:NSNetService, moreComing:boolean) {
console.log(`netServiceBrowserDidFindServiceMoreComing, found service ${service.name}`);
console.log(`moreComing: ${moreComing}`);
this._callback(service.name);
}
}
然后在提供者中这样的事情:
@Injectable()
export class BonjourProvider {
private netServiceBrowser:NSNetServiceBrowser;
constructor(private config:ConfigProvider) {
this.netServiceBrowser = NSNetServiceBrowser.new();
}
public startDeviceDiscovery() {
this.netServiceBrowser.delegate = MyNSNetServiceBrowserDelegate.new().initWithCallback(result => console.log(`MyNSNetServiceBrowserDelegate callback: ${result}`));
// this.netServiceBrowser.searchForRegistrationDomains();
this.netServiceBrowser.searchForServicesOfTypeInDomain('_http._tcp', 'local');
}
}