gpt4 book ai didi

swift - 在 Swift 应用程序上使用 NSNetService 和 NSNetServiceBrowser

转载 作者:可可西里 更新时间:2023-11-01 00:53:50 25 4
gpt4 key购买 nike

我想制作一个能够发现并连接到本地网络上的对等点的应用程序,因此我决定使用 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

委托(delegate)是胶水代码,它简单地打印每次对控制台的调用。

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 之外,两个委托(delegate)中的回调都没有被调用 :-(

经过大量调试(并阅读了 stackoverflow 上的内容)后,我不明白为什么它不起作用。有任何想法吗 ?

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

最佳答案

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

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
}

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

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")
}

}

关于swift - 在 Swift 应用程序上使用 NSNetService 和 NSNetServiceBrowser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com