3

我想制作一个能够发现并连接到本地网络上的对等点的应用程序,所以我决定使用 bonjour 框架快速实现它。

但是,我无法使用 Swift 让 Bonjour 工作,我不知道为什么。这是我用来测试此服务的代码:

import Foundation

let BM_DOMAIN = "local"
let BM_TYPE = "_helloworld._tcp."
let BM_NAME = "hello"
let BM_PORT : CInt = 6543

/// Netservice
let nsns = NSNetService(domain: BM_DOMAIN, 
        type: BM_TYPE, name: BM_NAME, port: BM_PORT)
let nsnsdel = BMNSDelegate() //see bellow
nsns.delegate = nsnsdel
nsns.publish()

/// Net service browser.
let nsb = NSNetServiceBrowser()
let nsbdel = BMBrowserDelegate() //see bellow
nsb.delegate = nsbdel
nsb.searchForServicesOfType(BM_TYPE, inDomain: BM_DOMAIN)

println("press enter")
// this prevents the app from quitting instantly.
NSFileHandle.fileHandleWithStandardInput().availableData 

代表是胶水代码,它简单地打印对控制台的每个调用。

class BMNSDelegate : NSObject, NSNetServiceDelegate {
    func netServiceWillPublish(sender: NSNetService!) {
        println("netServiceWillPublish:sender");
    }
    // .....and so on for the 8 other methods.....
}

class BMBrowserDelegate : NSObject, NSNetServiceBrowserDelegate {
    func netServiceBrowserWillSearch(aNetServiceBrowser: NSNetServiceBrowser!){
        println("netServiceBrowserWillSearch")
    }
    // .....and so on for the 6 other methods.....
}

这是此示例代码的输出:

netServiceWillPublish:sender
netServiceBrowserWillSearch
press enter

如果我使用Bonjour browser,我可以看到该服务已正确发布。然而,两个代表中的回调都没有被调用,除了**WillPublish那些:-(

经过激烈的调试(并阅读了stackoverflow),我不知道为什么它不起作用。有任何想法吗 ?

(我使用的是 Mac OS X 10.9.3 和 xcode 6.0 beta build 6A215l)

4

2 回答 2

5

如果没有您的完整代码,可能很难确定您的问题是什么。我怀疑您将变量/常量声明为函数的本地变量。当它们超出范围时,对服务的引用就超出了范围。这就是为什么您尝试了一个阻塞调用来请求来自 STDIN 的输入(以使事情停留在那里)。根据 Apple 文档,netService 和 netServiceBrowser 都隐式地与默认运行循环关联,因此您也不需要显式地这样做。与运行循环显式关联会导致程序卡住,这不是您想要的。此代码创建以下输出

netServiceWillPublish:<NSNetService 0x14522e00> local _helloworld._tcp. hello
netServiceBrowserWillSearch
netServiceDidPublish:<NSNetService 0x14522e00> local. _helloworld._tcp. hello
netServiceDidFindService

并且不会被阻塞或处于阻止程序正常运行的运行循环中。在 AppDelegate.swift 中

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var nsns:NSNetService?
var nsnsdel:BMNSDelegate?
var nsb:NSNetServiceBrowser?
var nsbdel:BMBrowserDelegate?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.


    let BM_DOMAIN = "local"
    let BM_TYPE = "_helloworld._tcp."
    let BM_NAME = "hello"
    let BM_PORT : CInt = 6543

    /// Netservice
    nsns = NSNetService(domain: BM_DOMAIN,
        type: BM_TYPE, name: BM_NAME, port: BM_PORT)
    nsnsdel = BMNSDelegate() //see bellow
    nsns?.delegate = nsnsdel
    nsns?.publish()

    /// Net service browser.
    nsb = NSNetServiceBrowser()
    nsbdel = BMBrowserDelegate() //see bellow
    nsb?.delegate = nsbdel
    nsb?.searchForServicesOfType(BM_TYPE, inDomain: BM_DOMAIN)

    //println("press enter")
    // this prevents the app from quitting instantly.
    // NSRunLoop.currentRunLoop().run()
    // NSFileHandle.fileHandleWithStandardInput().availableData

    return true
}

以及其他地方的委托回调......

class BMNSDelegate : NSObject, NSNetServiceDelegate {
func netServiceWillPublish(sender: NSNetService!) {
    println("netServiceWillPublish:\(sender)");
}

func netService(sender: NSNetService, didNotPublish errorDict: [NSObject : AnyObject]) {
    println("didNotPublish:\(sender)");
}

func netServiceDidPublish(sender: NSNetService) {
    println("netServiceDidPublish:\(sender)");
}

func netServiceWillResolve(sender: NSNetService) {
    println("netServiceWillResolve:\(sender)");
}

func netService(sender: NSNetService, didNotResolve errorDict: [NSObject : AnyObject]) {
    println("netServiceDidNotResolve:\(sender)");
}

func netServiceDidResolveAddress(sender: NSNetService) {
    println("netServiceDidResolve:\(sender)");
}

func netService(sender: NSNetService, didUpdateTXTRecordData data: NSData) {
    println("netServiceDidUpdateTXTRecordData:\(sender)");
}

func netServiceDidStop(sender: NSNetService) {
    println("netServiceDidStopService:\(sender)");
}

func netService(sender: NSNetService,
    didAcceptConnectionWithInputStream inputStream: NSInputStream,
    outputStream stream: NSOutputStream) {
        println("netServiceDidAcceptConnection:\(sender)");
}
}

class BMBrowserDelegate : NSObject, NSNetServiceBrowserDelegate {

func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
                            didFindDomain domainName: String,
                            moreComing moreDomainsComing: Bool) {
    println("netServiceDidFindDomain")
}

func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
                            didRemoveDomain domainName: String,
                            moreComing moreDomainsComing: Bool) {
    println("netServiceDidRemoveDomain")
}

func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
    didFindService netService: NSNetService,
    moreComing moreServicesComing: Bool) {
        println("netServiceDidFindService")
}

func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
    didRemoveService netService: NSNetService,
    moreComing moreServicesComing: Bool) {
        println("netServiceDidRemoveService")
}

func netServiceBrowserWillSearch(aNetServiceBrowser: NSNetServiceBrowser!){
    println("netServiceBrowserWillSearch")
}

func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
    didNotSearch errorInfo: [NSObject : AnyObject]) {
        println("netServiceDidNotSearch")
}

func netServiceBrowserDidStopSearch(netServiceBrowser: NSNetServiceBrowser) {
    println("netServiceDidStopSearch")
}

}
于 2014-12-03T22:57:17.003 回答
2

NSNetServiceBrowser需要一个运行循环来执行。而不是从标准输入读取,调用NSRunLoop.currentRunLoop().run().

于 2014-07-07T20:15:57.127 回答